Error Code Implicit Euler Method

2 vues (au cours des 30 derniers jours)
Ethem TEKE
Ethem TEKE le 22 Oct 2019
Hello,
I have a problem about my code. I take a error code "Error in Untitled (line 18) l(x+1)=l(x)-(((c*h)/3)*l(x+1))-16*m(x+1)*h"
How can I correct this?
Thank you from now.
clear all
c=30;
u(1)=1;
v(1)=-2;
h=0.1;
m(1)=1;
l(1)=-2;
%explicit euler
for n=1:10
u(n+1)=u(n)+(h*v(n))
v(n+1)=v(n)-(((c*h)/3)*v(n))-16*u(n)*h;
n=1:11;
end
%implicit euler
for x=1:10
m(x+1)=m(x)+(h*l(x))
l(x+1)=l(x)-(((c*h)/3)*l(x+1))-16*m(x+1)*h;
end
  1 commentaire
darova
darova le 22 Oct 2019
Maybe you made a mistake
321.png

Connectez-vous pour commenter.

Réponse acceptée

ME
ME le 22 Oct 2019
The problem in the code itself is that in
l(x+1)=l(x)-(((c*h)/3)*l(x+1))-16*m(x+1)*h;
the l(x+1) term exceeds your matrix dimension, i.e. you only have l defined up to l(x) and you are trying to use l(x+1) in the calculation.
A slightly larger problem in your question is that you have not correctly defined your implicit solution scheme. For your set of equations
(assuming I have worked that out correctly) your numerical scheme should look more like:
which will be more complicated to solve than the scheme you lay out above because you would need to know both and in advance. As such this would usually be solved using either matrix or iterative solution methods.
If instead you wanted to go for a semi-implicit method then you could simply change the l(x+1) in your code to l(x).Or a final option would be to alternate the order of your equations on each time step. That way you would alternate which variable is being calculated explicitly and which is calculated implicitly.
I hope this all helps.
  1 commentaire
Vishwarajsinh Gohil
Vishwarajsinh Gohil le 22 Nov 2020
can you please help with iterative solution method. How do we plugin them here?
Thanks,

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by