MATLAB: Error Code Implicit Euler Method

error code

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

Best Answer

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.