Why wont my code stop running?
2 views (last 30 days)
Show older comments
I am trying to graph a direction field with a given differential equation and graph an approximate solution curve for two given points. I have got the direction field to graph on its own, but when i code the solution curves it makes the code keep running without showing a graph or stopping. No error code has popped up and im not sure what im missing.
the differential equation given is d(dy/dx)=-x
the points are y(1)=1 and y(0)=4
[x,y] = meshgrid(-5:0.5:5,-5:0.5:5);
dy = -x./y;
dx = ones(size(dy));
r = sqrt(dx.^2+dy.^2);
quiver(x,y,dx./r,dy./r,0.5);
grid on
hold on
y_prime = @(t,y)([1;-y(1)/y(2)]);
x0 = 1;
y0 = 1;
[tout,yout] = ode45(y_prime,[0 -10],[x0 y0]);
[tout,yout2] = ode45(y_prime,[0 10],[x0 y0]);
plot(yout(:,1),yout(:,2),'g');
plot(yout2(:,1),yout2(:,2),'g');
hold on
y_prime1 = @(t,y)([1;-y(1)/y(2)]);
x1 = 0;
y1 = 4;
[tout,yout3] = ode45(y_prime1,[0 -10],[x1 y1]);
[tout,yout4] = ode45(y_prime1,[0 10],[x1 y1]);
plot(yout(:,1),yout(:,2),'r')
plot(yout2(:,1),yout2(:,2),'r')
axis([-5 5 -5 5])
xlabel('x-axis')
ylabel('y-axis')
title('Directional field and Two IVP solutions for ODE ydy/dx=-x')
Answers (1)
Alan Stevens
on 6 Feb 2021
Edited: Alan Stevens
on 6 Feb 2021
Not totally clear what you are trying to do! Are you integrating wrt x or t?
The solution to dy/dx = -x/y is x^2 + y^2 = r^2 i.e. the equation of a circle.
For your first case, xo=1. y0=1, then r = 1, and for your second case, x0=0, y0=4, then r = 4.
0 Comments
See Also
Categories
Find more on Numerical Integration and Differential Equations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!