I plot an x , y graph can i ask the programme to tell me the value of y for the value of x i want ? how i can write these at the command window ?

1 comentario

Nastaran kh
Nastaran kh el 12 de Nov. de 2017
if you plot, in section tools,basic fitting you can evaluate

Iniciar sesión para comentar.

 Respuesta aceptada

Image Analyst
Image Analyst el 10 de Sept. de 2013

14 votos

I agree with Jan - it's ambiguous. You already have x, and y since you plotted it, so there's no need to extract anything from the axes (graph) at all. So in that case, I'd just use the x and y which are already available, and if the x is in the x array that you plotted, you can do this:
index = find(x == desiredXValue); % May be multiple indexes, possibly
yDesired = y(index);
Now, if the desired x is not in your x array, then you can use interp1() to get the interpolated/estimated y value for that x.
yDesired = interp1(x,y, desiredXValue);

11 comentarios

lclk psdk
lclk psdk el 15 de Sept. de 2013
i am so sorry for complexity the problem is that i plotted a figure of x , y then i want the program automatically uses the y value (any value) when its x value used (it is used in a loop) thanks alot for ur care
Image Analyst
Image Analyst el 15 de Sept. de 2013
Isn't that exactly what I just showed you how to do? In fact I showed you how to do it two different ways. Explain why my code does not do what you want.
lclk psdk
lclk psdk el 15 de Sept. de 2013
thank u alot ,,, i found ur answer very useful,,,,,,, i only wanted to explain my question
George Black
George Black el 8 de Sept. de 2017
Man, I just spent so long trying to figure this out. such a simple issue, but I am a Matlab novice. Great explanation.
jakub rolny
jakub rolny el 8 de Abr. de 2021
Thank you so much, helped a lot :)
Chee Xiang Chong
Chee Xiang Chong el 14 de Abr. de 2021
thanks
Koosha
Koosha el 1 de Feb. de 2022
Thank u so much.
DEEPIKA E
DEEPIKA E el 21 de Abr. de 2023
can you please say how it would be if I want to find x value corresponding to y value using interp1? @Image Analyst
Image Analyst
Image Analyst el 21 de Abr. de 2023
% Find the index of yDesired closest to your target y value
[~, index] = min(abs(yDesired - yTarget))
xTarget = x(index); % Or xDesired(index) if you used an array in interp1()
DEEPIKA E
DEEPIKA E el 21 de Abr. de 2023
Thank you
Image Analyst
Image Analyst el 21 de Abr. de 2023
change is your y so
y = ((Temperature-Ti)/(Tf-Ti))*100
and x is time according to your plot(time, change) call. By the way, don't use time, or any other built-in function name as your variable name because it could lead to confusion and errors. And don't call one variable Time and another time - that's just downright confusing! But anyway, I can't figure out how your y depends on x. Can you give me an equation where y is a function of x? If you want Temperature as a function of change, it's simply
Temperature = (change/100) * (Tf - Ti) + Ti

Iniciar sesión para comentar.

Más respuestas (3)

Jan
Jan el 10 de Sept. de 2013
Editada: Jan el 10 de Sept. de 2013

4 votos

The question is not clear. If you plot x versus y, the values are known and therefore the problem has not relation to the plotting. But perhaps you have a Figure file and lost the x and y values. Please explain this by editing the original questions.
Do you want the y values exactly at the position of an x value, or are you interested in x values between two given x values also? In the first case consider, that numerical comparisons are bounded by the numerically limited precision:
x = 0:0.1:1;
find(x == 0.3) % does not find a matching element!
So perhaps this helps:
axes;
x = 0:0.1:pi;
y = sin(x);
plot(x, y);
clear('x', 'y'); % for demonstration only
LineH = get(gca, 'Children');
x = get(LineH, 'XData');
y = get(LineH, 'YData');
xx = 0.724 % Any value
yy = interp1(x, y, xx) % Linear interpolation [EDITED, not "linspace"]
xx2 = x(5); % One exact value
index = find(x == xx2); % Of course this is 5
disp(y(index))

5 comentarios

lclk psdk
lclk psdk el 15 de Sept. de 2013
i am so sorry for complexity the problem is that i plotted a figure of x , y then i want the program automatically uses the y value (any value) when its x value used (it is used in a loop) thanks alot for ur care
lclk psdk
lclk psdk el 15 de Sept. de 2013
thank u alot ,,, i found ur answer very useful,,,,,,, i only wanted to explain my question
lclk psdk
lclk psdk el 15 de Sept. de 2013
please why we wrote this,,,,,,,,,,,,,,,,, LineH = get(gca, 'Children'); x = get(LineH, 'XData'); y = get(LineH, 'YData');
Image Analyst
Image Analyst el 15 de Sept. de 2013
He's getting the data from the graph itself. It's as if you lost your original data, for example by calling the clear('x') command. If you did that unwise thing, then it's possible to extract your data from the graph, which has, in effect "stored" it.
lclk psdk
lclk psdk el 16 de Sept. de 2013
Editada: lclk psdk el 16 de Sept. de 2013
thank u very much............i am so happy to know such helpful men like u , Jan Simon and Ilham Hardy

Iniciar sesión para comentar.

Ilham Hardy
Ilham Hardy el 10 de Sept. de 2013

3 votos

h = gcf; %handle of current fig
axObj = get(h, 'Children');
datObj = get(axObj, 'Children');
xdata = get(datObj, 'XData');
ydata = get(datObj, 'YData');
Asimananda Khandual
Asimananda Khandual el 27 de En. de 2018

2 votos

Respected Image Analyst gave the best answer; I am just simplifying it with examples.
>> x=1:1:100; >> y=5*x+10; >> yDesired = interp1(x,y, 10)
yDesired =
60
>> xDesired = interp1(y,x, 60)
xDesired =
10
=====================================================================================
>> y=5*x.^2+2*x+10; >> plot(y,x) >> xDesired = interp1(y,x, 60)
xDesired =
2.9630
>> yDesired = interp1(x,y, 10)
yDesired =
530
-----------------HELPFILES---------------
Vq = interp1(X,V,Xq,METHOD) specifies alternate methods.
The default is linear interpolation. Use an empty matrix [] to specify
the default. Available methods are:
'nearest' - nearest neighbor interpolation
'linear' - linear interpolation
'spline' - piecewise cubic spline interpolation (SPLINE)
'pchip' - shape-preserving piecewise cubic interpolation
'cubic' - same as 'pchip'
'v5cubic' - the cubic interpolation from MATLAB 5, which does not
extrapolate and uses 'spline' if X is not equally
spaced.
Vq = interp1(X,V,Xq,METHOD,'extrap') uses the interpolation algorithm
specified by METHOD to perform extrapolation for elements of Xq outside
the interval spanned by X.
Vq = interp1(X,V,Xq,METHOD,EXTRAPVAL) replaces the values outside of the
interval spanned by X with EXTRAPVAL. NaN and 0 are often used for
EXTRAPVAL. The default extrapolation behavior with four input arguments
is 'extrap' for 'spline' and 'pchip' and EXTRAPVAL = NaN (NaN +NaNi for
complex values) for the other methods.
PP = interp1(X,V,METHOD,'pp') will use the interpolation algorithm specified
by METHOD to generate the ppform (piecewise polynomial form) of V. The
method may be any of the above METHOD except for 'v5cubic'. PP may then
be evaluated via PPVAL. PPVAL(PP,Xq) is the same as
interp1(X,V,Xq,METHOD,'extrap').

Categorías

Etiquetas

Preguntada:

el 10 de Sept. de 2013

Comentada:

el 21 de Abr. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by