It is possible to read the graph and it has to produce the Y value for my input X value.

조회 수: 3 (최근 30일)
This is what my program to create a graph.. now i want to read the graph and it has to produce the y value for my input x value. is there any syntax for that?
j=input('Enter the number of straight lines in the graph: ');
for i=1:j
disp('Enter the X Y co-ordinate of one end of the straight line:');
x1(i)=input(' ');
y1(i)=input(' ');
disp('Enter the X Y co-ordinate of another of the straight line:');
x2(i)=input(' ');
y2(i)=input(' ');
c(i)=input('Enter the Y-intercept for straight line: ');
m(i)=(y2(i)-y1(i))/(x2(i)-x1(i));
end
for i=1:j
x=x1(i):0.01:x2(i);
y=((m(i))*x)+c(i);
plot(x,y);%,'MarkerFaceColor','g');
hold on;
%axis([-10 10 -5 5])
end
disp('-----------------------');
disp('your graph is displayed');
disp('-----------------------');
disp('Enter the X value from your graph: ');
p=input(' ');
disp('Corresponding Y is: ');
  댓글 수: 2
Walter Roberson
Walter Roberson 2013년 1월 3일
It appears to me that you have multiple y values for each x.
Vivek
Vivek 2013년 1월 4일
편집: Vivek 2013년 1월 4일
hi walter,
this is what the assignment given to me
4. Consider the following graph with co-ordinates for some points. a) Create a function to find the equations for the graph. Create a function with X co-ordinate value as input for calculating the corresponding Y co-ordinate value as output. Implement the equations using m-scripting constructs, like For- loop, If-else, Switch-case etc. b) Plot the given graph using MATLAB inbuilt functions.
| --------
| /
|/
------------|-------------
/|
/ |
---------- |
I assume that now you understand the reason for multiple straight line. could you please help on this.

댓글을 달려면 로그인하십시오.

채택된 답변

Davide Ferraro
Davide Ferraro 2013년 1월 3일
Why are you asking two (x,y) values for a line and then also the intercept? Given two points you are able to calculate both m and c. And that point you will have the equation y of your line that you can evaluate to find the corresponding Y.
If you have more lines you may consider including and IF-ELSE statement, check what is the X value, associate it to the corresponding equation and then evaluate the equation. As Walter pointed out you may have multiple Y values, you may prevent this by checking that the user is using different X ranges when creating the curves.

추가 답변 (1개)

Dr. Seis
Dr. Seis 2013년 1월 3일
편집: Dr. Seis 2013년 1월 3일
Disregarding the fact that you can determine your c value from your m value, x1 value, and y1 value (or m, x2 and y2)... I think the syntax you are looking for (following your current syntax) is something like:
new_y = ... ; % Determine new y value for input x value
disp(sprintf('Corresponding Y is: %f',new_y));

카테고리

Help CenterFile Exchange에서 Specifying Target for Graphics Output에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by