Solve a second order differential equation

조회 수: 40 (최근 30일)
Ben
Ben 2013년 3월 7일
답변: Nicolas 2014년 1월 28일
Hi, I am completely new to Matlab and am looking to solve a simple second order differential equation:
y''+w^2*y=0 IC: y(0)=0, y'(0)=1 BC=[0,pi]
I am looking to solve for both y(x) and y'(x)
I understand this is a simple equation to solve and have done it fine on paper. However I have been trying different ways to solve it on matlab but to no avail. I have tried both dsolve and ode45 functions but did not quite understand what I was doing. Any help would be great. Thanks in advance, Ben

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2013년 3월 7일
편집: Azzi Abdelmalek 2013년 3월 7일
% x1=y
%x2=dy
Then
%dx1=dy=x2
%dx2=d2y=-w^2*y=-w^2*x1
save this function as yourfcn
function dx=yourfcn(t,x)
w=1
dx(1)=x(2)
dx(2)=-w^2*x(1)
Then call the ode45 function
[t,x]=ode45(@yourfcn,[0,pi],[0 0])
Then deduce y
y=x(:,1)

추가 답변 (2개)

Shashank Prasanna
Shashank Prasanna 2013년 3월 7일
I really recommend going through our documentation and these things become intuitive. Most standard setup's usually have examples in the documentation:
  댓글 수: 1
Ben
Ben 2013년 3월 8일
Thanks for the help guys. Didn't realise this documentation existed. Very useful. Thanks

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


Nicolas
Nicolas 2014년 1월 28일
Hi I have written the following First the Function
function h = goo(t,y)
h = zeros(3,1);
h(1) = y(2) * y(3);
h(2) = -y(1) * y(3);
h(3) = -0.51 * y(1) * y(2);
end
and then the code
clear all
initial=[-2 -3.5 21];
time=[0 10];
[t,y] = ode45(@goo,time,initial);
plot (t,y)
However I always get the following message:
Error using feval
Undefined function 'goo' for input arguments of type 'double'.
Error in odearguments (line 88)
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode45 (line 114)
[neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs, odeFcn, ...
Can anyone help me on this Thank you

카테고리

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