help me with this plss..( second order with euler method )

1 view (last 30 days)
y" = -5y - 5/4y'
i am not good in matlab and i try to solve this question but when i run it the value of yDn1 remain constant
pls anyon help me
yn = 0 ;
yD1 = 1 ;
y = 0
x = 0
h = 0.1
for i = 1:10
yn1 = yn + h*yDn1 - h^2*( 5*y + 5/(4*yD1))
yDn1 = yDn - h*( 5*y + 5/(4*yD1))
yn = yn1
yDn = yDn1
x = x + 0.1
end
fprintf ('yDn1 = %6.3 f/n', yDn1)
fprintf ('yn1 = %6.3 f/n', yn1)

Answers (1)

John D'Errico
John D'Errico on 16 Jul 2020
In addition to using the variable yDn1 before it was ever defined, your initial condition seems to be y. Thus you set:
y = 0
and you never change y, even though you use it inside the loop, as:
yn1 = yn + h*yDn1 - h^2*( 5*y + 5/(4*yD1))
yDn1 = yDn - h*( 5*y + 5/(4*yD1))
In those lines, we see y used. Not yn. Since y is always 0, this will cause a problem.
When you learn to program, you need to learn many important things. One such lesson is to think about the variables you use. Did you define them before they are used? Are you using the correct variables in your expressions?
Be careful when you use many copies of what is in your mind the same variable. For example, here we see y, yn, and yn1, all of which are essentially y from your differential equation, but in subtly different ways. The problem is, you need to get clear in your mind what those variables mean to you, and how to use them properly. If not, expect buggy code.

Products

Community Treasure Hunt

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

Start Hunting!