Hi, I’m new to Matlab. I've plot a graph with 3 different equations, it works fine, but now I need to get certain points from my y axis at a given x value to put into a different equation. How do I get that y value?

 Accepted Answer

Assuming that the equations do not have unique inverses, use interp1 with the numerical results of each equation, with:
xi = interp1(y, x);
for each equation, where x is the independent variable and y the dependent variable.
Note that if the dependent variables are not monotonically either increasing or decreasing, it will be necessary to choose an appropriate range around the dependent variable value you want to interpolate in order to get appropriate results for the independent variable.

10 Comments

Hi thanks for your answer, I’m still a bit confused. How and why do I need to put in a range? i have a range of x values that goes from 0-25 with an interval of 5. I would like the corresponding y values to each of those.
My pleasure!
I apparently misread your Question, assuming you wanted x-values for specific y-values.
If all your equations are functions (such as anonymous functions), you can simply provide whatever x-value you want and the function will calculate the associated y-value.
If they are not functions, and you do not want to write them as functions,, evaluate the equations at the x-values you provide, and use interp1 to provide y-values for any x-value you want, within the given range.
aha okay brilliant, my equations are functions. So how do i write that? I just tried f(5) which i assumed would be my function when x=5 but it came out with a weird number.
That’s the correct approach.
I have no idea what your equations are, so I can’t determine how ‘weird’ the result is. It could be that 5 is not within the range of values your function is designed to work with.
For example:
y = asin(5)
produces:
y =
1.570796326794897 - 2.292431669561178i
and will only produce real values for arguments on the interval (-1,1).
for i=1:length(t)%this is the equation
if t(i)<5
f(i) = 0.1553567*(t(i)^6)-2.0416*(t(i)^5)+9.1837*(t(i)^4)-14.829*(t(i)^3)-1.3703*(t(i)^2)+32.821*(t(i))-1.3155;
elseif t(i)>=5 && t(i)<15.4
f(i) = 0.003980879*(t(i)^5)-0.2247*(t(i)^4)+4.8682*(t(i)^3)-50.442*(t(i)^2)+254.67*(t(i))-430.66;
elseif t(i)>=15.4
f(i) = -0.073*(t(i)^2)+6.1802*(t(i))+40.423;
end
this is the code, if i try y=f(5), it says y=-0.2303, when it should be 62.17
When I do:
t = 5;
for i=1:length(t)%this is the equation
if t(i)<5
f(i) = 0.1553567*(t(i)^6)-2.0416*(t(i)^5)+9.1837*(t(i)^4)-14.829*(t(i)^3)-1.3703*(t(i)^2)+32.821*(t(i))-1.3155;
elseif t(i)>=5 && t(i)<15.4
f(i) = 0.003980879*(t(i)^5)-0.2247*(t(i)^4)+4.8682*(t(i)^3)-50.442*(t(i)^2)+254.67*(t(i))-430.66;
elseif t(i)>=15.4
f(i) = -0.073*(t(i)^2)+6.1802*(t(i))+40.423;
end
end
fval = f
I get:
fval =
62.16774687499997
I have no idea what you’re doing to get the other result.
Could you poossibly have two different functions called ‘f’ and you are seeing the result of the last one evaluated?
t is a range from 0-25, so i dont think i can put t=5;? because i aslo want t at 10, 15, 20 and 25. Sorry for dragging this out, do really appreciate your help though.
My pleasure!
I completely fail to understand the problems you are having.
This works:
t = 5:5:25;
for i=1:length(t)%this is the equation
if t(i)<5
f(i) = 0.1553567*(t(i)^6)-2.0416*(t(i)^5)+9.1837*(t(i)^4)-14.829*(t(i)^3)-1.3703*(t(i)^2)+32.821*(t(i))-1.3155;
elseif t(i)>=5 && t(i)<15.4
f(i) = 0.003980879*(t(i)^5)-0.2247*(t(i)^4)+4.8682*(t(i)^3)-50.442*(t(i)^2)+254.67*(t(i))-430.66;
elseif t(i)>=15.4
f(i) = -0.073*(t(i)^2)+6.1802*(t(i))+40.423;
end
end
fval = f(:)
producing:
fval =
6.216774687499998e+01
9.112789999999978e+01
1.176574906249974e+02
1.348270000000000e+02
1.493030000000000e+02
So it goes from 62.17 to 149.30.
okay, think i've got it now. Had a large bit of code that goes with it which i think was the problem. Thank you so much :)
As always, my pleasure!

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!