Values of intersection points of plot. Print results.
Mostrar comentarios más antiguos
Suppose I have a code that numerically integrates a set of ODEs, where my dependent variable is t and the vector of variables is x. I wish to print and plot values of x(1) for whenever x(2)=1. How might I achieve that? Some sort of "If" function?
In other words, graphically speaking, I can plot x(1) against x(2) and draw a vertical line at x(2)=1 and I want to know the x(1) values at the intersections of the plot and the vertical line.
Thanks.
3 comentarios
Cedric
el 22 de Abr. de 2013
I am not sure to fully understand your question.. you get a vector x and you want to print elements x(i-1) for all i such as x(i) equals 1?
Pauline
el 23 de Abr. de 2013
Walter Roberson
el 23 de Abr. de 2013
Saying x(:,1) and x(:,2) would make the question clearer.
Respuesta aceptada
Más respuestas (1)
EDIT: this answer is wrong, I answered too quickly without paying attention to the fact that your x is the output of an ODE solver. Please jump directly to Teja's answer.
Ok, then you can proceed as follows:
id = x(:,2) == 1 ;
x(id,1) % This will provide you with all x(:,1) that
% correspond to a x(:,2) equal to 1.
id is a vector of logicals (outcome of the test of equality on x(:,2)), that we use for indexing x(:,1). If you want to have the positions numerically, you can use
find(id)
3 comentarios
Walter Roberson
el 23 de Abr. de 2013
And please keep in mind, http://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F
Ah yes, if x gets out of an ODE solver, Walter's comment and Teja's answer are really important; I should have thought a little further.
EDIT: and my answer is even wrong if you don't interpolate, because it is absolutely not trivial to quantify how far from 1 points that are from each side of 1 can be. Setting a large tolerance would be wrong too as it could lead to taking points that are not around any 1 crossing.
Pauline
el 23 de Abr. de 2013
Categorías
Más información sobre Ordinary Differential Equations en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!