How to graph it

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 个评论

José-Luis
José-Luis 2014-5-2
编辑:José-Luis 2014-5-2
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.
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,'*');

请先登录,再进行评论。

回答(2 个)

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 2014-5-3

0 个投票

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 个评论

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.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Programming 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by