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.
Kennedy
Kennedy 2014년 5월 3일
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개)

Image Analyst
Image Analyst 2014년 5월 2일

0 개 추천

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

Image Analyst
Image Analyst 2014년 5월 3일
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에 대해 자세히 알아보기

질문:

2014년 5월 2일

댓글:

2014년 5월 3일

Community Treasure Hunt

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

Start Hunting!

Translated by