Plot function to graph differential equations

조회 수: 4 (최근 30일)
Aleem Andrew
Aleem Andrew 2020년 2월 5일
댓글: Star Strider 2020년 2월 6일
I am trying to plot the solution to the following system but the code below plots a small circle instead of a parabola, which is the solution to the system. Can anyone explain how to plot the solution (y= f(x)) or point out any errors in the code? Thank you
tspan = [0 3 5 6];
y0 = [0 4 5 6];
[x,y] = ode45(@(x,y) odefcn(x,y,A,B), tspan,y0);
plot(x,y(:,1),'-o',x,y(:,2),'-.')
function dydx = odefcn(x,y,A,B)
dydx = zeros(4,1);
dydx(1) = y(2);
dydx(3)= y(4);
dydx(2) = y(2)*sqrt(y(1)*y(1)+ (y(3)*y(3))); %this is a system of two second order differential equations
dydx(4) = y(3)*sqrt((y(1)*y(1)+ (y(3)*y(3))-9.81;
end
  댓글 수: 2
darova
darova 2020년 2월 6일
Attach the original equations
Aleem Andrew
Aleem Andrew 2020년 2월 6일
The original equations are: x"=x'*sqrt(x'^2+y'^2); y"=y'*sqrt(x'^2+y'^2)-9.81;

댓글을 달려면 로그인하십시오.

채택된 답변

Star Strider
Star Strider 2020년 2월 6일
Use a different tspan vector.
When I run your code (using random values for ‘A’ and ‘B’, since they are not supplied), I get:
Warning: Failure at t=5.659410e-01. Unable to meet integration tolerances without reducing the
step size below the smallest value allowed (1.776357e-15) at time t.
With:
tspan = [0 0.5];
I get a plot.
Also, ‘dx(4)’ as posted is missing a couple parentheses. This corrected version works:
dydx(4) = y(3)*sqrt((y(1)*y(1))+ (y(3)*y(3)))-9.81;
Be sure that is the syntax you intended.
  댓글 수: 4
Aleem Andrew
Aleem Andrew 2020년 2월 6일
Ok I will look into that for further details such as why the order of the values needs to be ascending Thank you
Star Strider
Star Strider 2020년 2월 6일
As always, my pleasure!
See the documentation section on tspan. Note that: ‘The elements in tspan must be all increasing or all decreasing.’ So they only need to be monotonic.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by