필터 지우기
필터 지우기

Cank-Nicolosn to solve a nonlinear PDE

조회 수: 2 (최근 30일)
Hana Bachi
Hana Bachi 2022년 3월 3일
답변: SAI SRUJAN 2023년 11월 10일
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?

답변 (1개)

SAI SRUJAN
SAI SRUJAN 2023년 11월 10일
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.

카테고리

Help CenterFile Exchange에서 Boundary Conditions에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by