Why wont my code stop running?
조회 수: 1 (최근 30일)
이전 댓글 표시
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')
답변 (1개)
Alan Stevens
2021년 2월 6일
편집: Alan Stevens
2021년 2월 6일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Numerical Integration and Differential Equations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!