Error Code Implicit Euler Method

4 次查看(过去 30 天)
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

采纳的回答

ME
ME 2019-10-22
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 个评论
Vishwarajsinh Gohil
Vishwarajsinh Gohil 2020-11-22
can you please help with iterative solution method. How do we plugin them here?
Thanks,

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by