Hi Hana,
I understand that you are facing an error in solving a non linear PDE using Crank-Nicolson method. The encountered error message indicates that the index being used in line 42 is exceeding the bounds of the array. The error is being triggered by the following code segment.
% declaration of 'f' as a function handle
f = @(x) phi(x,t(1)); % Initial condition
g1 = @(t)phi(x(1),t); % Left boundary condition
g2 = @(t)phi(x(N+1),t); % Right boundary condition
r = dt / (2*dx^2);
a = -0.003*r;
b = 1 + 2*0.003*r;
c = -r*0.003;
d =(1+0.003*2*r);
e =1+0.003*2*r;
% declaration of 'f' as variable of type double.
f =0.003*r;
% 6 Implementation of Crank-Nicolson method
u = zeros(N+1,M+1);
u(2:N,1) = f(x(2:N)); % Put in the initial condition
We can see that the issue stems from the variable 'f' being redefined as a double after initially being declared as a function handle. This inconsistency leads to the error in line 42 where the code attempts to use 'f' as a function handle.
To resolve this problem, you should consider renaming either the function handle or the double variable to resolve the issue and ensure that the correct variable is used in line 42.
I hope this helps.