Asked by oday hazaimah
on 2 Jul 2019

I am trying to run a code for solving 2nd order ODE but I have no idea where is my mistake in the following code:

function f=f(t0,x0,y0,N)

h=0.01;

N=5;

t0=0;

x0=0;

y0=0;

H=zeros(1,N+1);

T=zeros(1,N+1);

X=zeros(1,N+1);

Y=zeros(1,N+1);

H(1)=h; T(1)=t0; X(1)=x0; Y(1)=y0;

for j=1:N

T(j+1)=T(j)+H(j);

X(j+1)=X(j)+H(j)*Y(j);

Y(j+1)=Y(j)+H(j)*ahat(T(j),X(j),Y(j));

H(j+1)=H(j)*(1-H(j));

end

plot [T X]

plot [T Y]

Answer by Vaibhav Tomar
on 4 Jul 2019

Can you please specify the values of T, Y and X. Without that it's difficult to say why the code is throwing errors. I'd suggest to look for an example for second order ODE algorithm.

You can try this alternate code:

% 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)

oday hazaimah
on 4 Jul 2019

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 15 Comments

## Jon (view profile)

## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/469936-euler-s-method-for-2nd-order-ode#comment_720650

## infinity (view profile)

## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/469936-euler-s-method-for-2nd-order-ode#comment_720651

## Geoff Hayes (view profile)

## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/469936-euler-s-method-for-2nd-order-ode#comment_720655

## oday hazaimah (view profile)

## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/469936-euler-s-method-for-2nd-order-ode#comment_720672

## infinity (view profile)

## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/469936-euler-s-method-for-2nd-order-ode#comment_720688

## oday hazaimah (view profile)

## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/469936-euler-s-method-for-2nd-order-ode#comment_721022

## Geoff Hayes (view profile)

## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/469936-euler-s-method-for-2nd-order-ode#comment_721158

## oday hazaimah (view profile)

## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/469936-euler-s-method-for-2nd-order-ode#comment_721198

## Geoff Hayes (view profile)

## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/469936-euler-s-method-for-2nd-order-ode#comment_721393

## oday hazaimah (view profile)

## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/469936-euler-s-method-for-2nd-order-ode#comment_721441

## Geoff Hayes (view profile)

## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/469936-euler-s-method-for-2nd-order-ode#comment_721474

## oday hazaimah (view profile)

## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/469936-euler-s-method-for-2nd-order-ode#comment_721475

## Geoff Hayes (view profile)

## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/469936-euler-s-method-for-2nd-order-ode#comment_721510

## oday hazaimah (view profile)

## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/469936-euler-s-method-for-2nd-order-ode#comment_721517

## Geoff Hayes (view profile)

## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/469936-euler-s-method-for-2nd-order-ode#comment_721707

Sign in to comment.