Can't see my text on a plot, but only for some values

Hey there, I'm trying to put on a text on the max value in a graph of solutions for second ODE. The equations depends on a variable n. In some cases I can see the text on the graphs and in others I can't - depend what is the value of n.
this is my code and my text info, the odefun function gives a solution for this ODE :
x^2*y''+x*y'+(x^2-n^2)=0
[x, Y]= ode45(@odefun, [0.5, 20], [1 1]);
figure;
plot(x,Y);
title('Y as a function of x');
xlabel('x');
ylabel('Y(x)');
max_value=max(Y)
max_x=max_value(1)
max_y=max_value(2)
txt='Max Value';
text(max_x, max_y, txt,'FontSize', 10, 'color', 'red', 'VerticalAlignment', 'top');
end

1 commentaire

Rik
Rik le 5 Jan 2018
The only thing I can think of is that the solution might be outside of your axis area, but that seems unlikely.

Connectez-vous pour commenter.

 Réponse acceptée

When I used the Symbolic Math Toolbox to create ‘odefun’, the problem became obvious. You are using the two ‘Y’ values to define the x and y coordinates for your text object.
Try this slight variation:
n = 4.2;
odefun = @(x,Y,n)[Y(2);-1.0./x.^2.*(x.*Y(2)-n.^2+x.^2)];
[x, Y]= ode45(@(x,Y)odefun(x,Y,n), [0.5, 20], [1 1]);
figure;
plot(x,Y);
title('Y as a function of x');
xlabel('x');
ylabel('Y(x)');
[max_value,idx] = max(Y);
max_x=max_value(1);
max_y=max_value(2);
txt='Max Value';
text(x(idx), max_value, txt,'FontSize', 10, 'color', 'red', 'VerticalAlignment', 'top', 'HorizontalAlignment','center')

3 commentaires

You get two results, one for ‘Y(:,1)’, and one for ‘Y(:,2)’. If you are only plotting ‘Y(:,1)’, change the ‘max_value’ assignment to:
[max_value,idx] = max(Y(:,1));
The rest of my code remains the same, and you will only get one ‘Max Value’ text displayed on your plot.
Thank you!
My pleasure!

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by