Azzera filtri
Azzera filtri

Cank-Nicolosn to solve a nonlinear PDE

2 visualizzazioni (ultimi 30 giorni)
Hana Bachi
Hana Bachi il 3 Mar 2022
Risposto: SAI SRUJAN il 10 Nov 2023
Hello,
I'm trying to run a code to solve a nonlinear PDE using Crank-Nicolson method. but I'm getting an error message in line 42 says:
Index in position 1 exceeds array bounds. Index must not exceed 1.
Error in Cp42nd (line 41)
u(2:N,1) = f(x(2:N,1)); % Put in the initial condition.
Removing this conditiong and runing the code gave me a total mess for the the numerical solution as it shows below. any advice please?

Risposte (1)

SAI SRUJAN
SAI SRUJAN il 10 Nov 2023
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.

Community Treasure Hunt

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

Start Hunting!

Translated by