How to graph it
1 view (last 30 days)
Show older comments
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
Answers (2)
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.
1 Comment
Image Analyst
on 2 May 2014
Kennedy
on 3 May 2014
1 Comment
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.
See Also
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!