Second Order ODE solved with ODE45

조회 수: 4 (최근 30일)
Henrik
Henrik 2014년 10월 25일
댓글: Star Strider 2014년 10월 25일
Hi!
I am struggling with the task to solve a 2'nd order Ode with ODE45. Please help as I'm struggling with this.
Equation as given in the task:
y'' + pi*y^(x/3)*(2y' sin pi*x + pi*y cos pi*x) − y/9 = 0, y(0) = and y'(0) = -⅓
My code:
%Lab 2 uppgift 3
clear all
close all
clc
[t,y] = ode45(@odefun, t, [0, 2.5], [1, -1/3]);
plot(t,y(:,1),'-',t,y(:,2),'--')
title('Title');
xlabel('time t');
ylabel('solution y');
legend('y_1','y_2')
%The function called odefun.
function [dydx] = odefun(t,y)
%x = (0:0.1:2.5);
dydt=[y(2); pi*y(1).*exp(x/3)*2*y(2)*sin(pi*x) + x*y(1)*cos(pi*x) - (y(1)./9)x];
end
Thanks! /Henrik

채택된 답변

Star Strider
Star Strider 2014년 10월 25일
Your equation implies (to me) that y is a function of x, not t. So your function should probably be:
function [dydx] = odefun(x,y)
Also, there is an error in this line at the very end:
dydt=[y(2); pi*y(1).*exp(x/3)*2*y(2)*sin(pi*x) + x*y(1)*cos(pi*x) - (y(1)./9)x];
You might want:
dydt=[y(2); pi*y(1).*exp(x/3)*2*y(2)*sin(pi*x) + x*y(1)*cos(pi*x) - (y(1)./9).*x];
or some other operator, since MATLAB will throw an error without an operator there.
When I ran it (with the change in the function line, vectorising, and putting a multiplication operator before the x at the end, it ran.
  댓글 수: 2
Henrik
Henrik 2014년 10월 25일
Thanks a lot star strider!
The plot I get doesn't look right but I get it running at least. However with an error message saying:
"Warning: Failure at t=1.425454e+00. Unable to meet integration tolerances without reducing the step size below the smallest value allowed (3.552714e-15) at time t."
If you have any ideas to get rid of it I'm even more grateful.
Thanks //Henrik
Star Strider
Star Strider 2014년 10월 25일
My pleasure!
I re-coded your equation and found some errors. This seems to produce a much more reasonable result:
odefun = @(x,y) [y(2); -pi*y(1).^(x/3).*2.*y(2).*sin(pi*x) - x.*y(1).*cos(pi*x) + (y(1)./9).*x];
[t,y] = ode15s(odefun, [0, 2.5], [1, -1/3]);
plot(t,y(:,1),'-',t,y(:,2),'--')
title('Title');
xlabel('time t');
ylabel('solution y');
legend('y_1','y_2','Location','NW')
I also repositioned your legend so that it would not be on top of your plotted curves.

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

추가 답변 (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