5 views (last 30 days)

y''+1=0

ddy/ddt + 1 =0

how can i resolve this equation with runge kutta method

James Tursa
on 22 Jan 2020

Edited: James Tursa
on 23 Jan 2020

You've got a 2nd order equation, so that means you need a 2-element state vector. The two states will be y and y'. All of your code needs to be rewritten with a 2-element state vector [y;y'] instead of the single state y. I find it convenient to use a column vector for this. So everywhere in your code that you are using y, you will need to use a two element column vector instead. E.g., some changes like this:

y = zeros(2,numel(t)+1); % pre-allocate the solution, where y(:,m) is the state at time t(m)

:

y(:,1) = [1;0]; % need to initialize two elements at first time, position and velocity

:

f = @(t,y)[y(2);-1]; % [derivative of position is velocity; derivative of velocity is acceleration = -1]

:

k1 = h*feval(f, t , y(:,m) ); % changed y(m) to y(:,m)

Also, you have a bug in the line that combines the k's ... you need (1/6)* instead of (1/6)+

Make an effort at implementing these changes and then come back with any problems you continue to have.

Sign in to comment.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 6 Comments

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/501501-how-can-i-resolve-this-equation-runge-kutta-method#comment_788812

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/501501-how-can-i-resolve-this-equation-runge-kutta-method#comment_788812

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/501501-how-can-i-resolve-this-equation-runge-kutta-method#comment_788823

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/501501-how-can-i-resolve-this-equation-runge-kutta-method#comment_788823

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/501501-how-can-i-resolve-this-equation-runge-kutta-method#comment_788828

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/501501-how-can-i-resolve-this-equation-runge-kutta-method#comment_788828

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/501501-how-can-i-resolve-this-equation-runge-kutta-method#comment_788831

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/501501-how-can-i-resolve-this-equation-runge-kutta-method#comment_788831

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/501501-how-can-i-resolve-this-equation-runge-kutta-method#comment_788834

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/501501-how-can-i-resolve-this-equation-runge-kutta-method#comment_788834

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/501501-how-can-i-resolve-this-equation-runge-kutta-method#comment_788835

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/501501-how-can-i-resolve-this-equation-runge-kutta-method#comment_788835

Sign in to comment.