Finite difference method solving boundary condition

Hi try to solve the nonlinear ordinary difference equation by finite difference method with boundary conditions: U(-1) = 0, U(1)=0. The code below.
Function [r, sol] =res(U)
n = 10;
r = ((1:n)-1)/(n-1);
r0=zeros(n+1,1);
color={'r','m','c','b');
For delta = 2:5
Sol = solve(@(r)fun(r, n), r0);
norm(fun(sol, n));
Plot(r, sol(1:n), color{delta-1});hold on;
end
legend('delta=2', 'delta=3','delta=4','delta=5');
Function res=fun(z, n)
lambda =0.1;
beta = 0.1;
eps = 0.1;
m=0;
r = ((1:n)-1)/(n-1);
h=1/(n-1);
U = z(1:n);
res_U = zeros(n, 1);
res_U(1)=0;
For i=2:n-1
res_U(i)=((1+beta*U(i))/h^2)*(U(i+1)-2*U(i)+U(i-1))+((1+beta*U(i))/(r*h))*(U(i+1)-U(i-1))+beta*((U(i+1)-U(i-1))/h)^2+lambda*((1+eps*U(i))^m)*exp(U(i)/(1+eps*U(i)))-delta*U(i);
end
res_U(n)=0;
res = res_U;
end
Outcome
>> res
Warning: Trust-region-dogleg algorithm of Fsolve can not handle non-square systems; using levenberg-marquardt algorithm instead.
Equation solved. Fsolve completed because the vector of function values is near zero as measured by the default value of the function tolerance, and the problem appears regular as measured by the gradient.
'Please help me correct this errors'

5 件のコメント

Torsten
Torsten 2021 年 5 月 22 日
編集済み: Torsten 2021 年 5 月 22 日
The message you got is not an error message ; Fsolve finished successfully.
Do the values returned in the Sol vector make sense for you ?
Oluwaseun Adisa
Oluwaseun Adisa 2021 年 5 月 22 日
But how can I convert boundary condition: U(-1) = 0; U(1) = 0; to initial value r0 =?
Oluwaseun Adisa
Oluwaseun Adisa 2021 年 5 月 28 日
About the codes above not satisfy my result very well and what I discovered is that the boundary conditions not function in equations because if I delete the boundary conditions and run the program the program will still plot the same graph out. Anybody help me out
Torsten
Torsten 2021 年 5 月 28 日
Is it necessary to write your own code ? If not, you should use sophisticated matlab code especially designed for this problem class, i.e. bvp4c in your case.
Oluwaseun Adisa
Oluwaseun Adisa 2021 年 6 月 5 日
How can I get that one?

サインインしてコメントする。

回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeSystems of Nonlinear Equations についてさらに検索

質問済み:

2021 年 5 月 22 日

コメント済み:

2021 年 6 月 5 日

Community Treasure Hunt

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

Start Hunting!

Translated by