How to graph it

1 view (last 30 days)
Kennedy
Kennedy on 2 May 2014
Commented: Image Analyst on 3 May 2014
You are required to design, code, and test a Matlab program that performs the followings:
1. Request the user to enter a function
2. Request the user to enter the minimum and maximum values the function can take (the function domain)
3. Request the user to enter the number of points
4. Plot the graph of the function on the function domain using the number of points given
How to do this? What does this question wants? Anyone can explain in more details, including the codes as well.
  2 Comments
José-Luis
José-Luis on 2 May 2014
Edited: José-Luis on 2 May 2014
Please read the documentation. The function input() will go a long way in helping you. Then please look at the plot() function. Very few people here would be willing to do your homework, so please show some effort.
Kennedy
Kennedy on 3 May 2014
Please check if I am doing the right thing.
func=input('Please enter a function\n');
xmin=input('Input the minimum point: \n');
xmax=input('Input the maximum point: \n');
points=input('Please enter number of points\n');
x=[xmin:xmax/points:xmax];
y=func;
plot(x,y,'*');

Sign in to comment.

Answers (2)

Image Analyst
Image Analyst on 2 May 2014
You can use inputdlg(). Here's a snippet you might use. Of course you could simplify it some if you don't require integers:
% Ask user for a number.
defaultValue = 45;
titleBar = 'Enter a value';
userPrompt = 'Enter the integer';
caUserInput = inputdlg(userPrompt, titleBar, 1, {num2str(defaultValue)});
if isempty(caUserInput),return,end; % Bail out if they clicked Cancel.
% Round to nearest integer in case they entered a floating point number.
integerValue = round(str2double(cell2mat(caUserInput)));
% Check for a valid integer.
if isnan(integerValue)
% They didn't enter a number.
% They clicked Cancel, or entered a character, symbols, or something else not allowed.
integerValue = defaultValue;
message = sprintf('I said it had to be an integer.\nI will use %d and continue.', integerValue);
uiwait(warndlg(message));
end
You can use inputdlg() to ask for a string (the equation they're supposed to type in) also. You can even do that (steps 1, 2, and 3) all on the same dialog box.
You could ask the user to put in a string like a polynomial in x, then use strrep to replace x with the actual numbers and then use eval() to carry out the equation for the range of numbers.

Kennedy
Kennedy on 3 May 2014
Please check if I am doing the right thing.
func=input('Please enter a function\n');
xmin=input('Input the minimum point: \n');
xmax=input('Input the maximum point: \n');
points=input('Please enter number of points\n');
x=[xmin:xmax/points:xmax];
y=func;
plot(x,y,'*');
  1 Comment
Image Analyst
Image Analyst on 3 May 2014
No, it doesn't look like it. For example, what is func? It's just a string. Doesn't look like you read the last sentence of my answer. Also you might use linspace to create x instead of what you did.

Sign in to comment.

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!