Finite difference method for second order ode

2 Ansichten (letzte 30 Tage)
Oluwaseun Adisa
Oluwaseun Adisa am 16 Mai 2021
Kommentiert: Jan am 16 Mai 2021
Hi. I am trying to solve nonlinear equation using fsolve and repeat graph bet four times with different value. Here is my code:
Function main n = 10 r0 = ones(n, 1) For bet = 0.1:0.1:0.4 Sol = solve(@(r)fun(r, n), r0) norm(fun(sol, n)) end r = ((1:n)-1)/(n-1) Plot(r, sol(0.4,:),'g-*',sol(0.3,:),'m-*',sol(0.2,:),'b-*',sol(0.1,:),'r-*') end
  3 Kommentare
Oluwaseun Adisa
Oluwaseun Adisa am 16 Mai 2021
using fsolve. Here is my code: Function main n = 10 r0 = ones(n, 1) Sol = solve(@(r)fun(r, n), r0) norm(fun(sol, n)) r = ((1:n)-1)/(n-1) Plot(r, sol(1:n)) end
Outcome >> main Solver stopped prematurely Fsolve stopped because it exceeded the function evaluation limit, options.MaxFunEvals = 1000(the default value).
Jan
Jan am 16 Mai 2021
I've mentioned it already: Your code would not run at all, but stop due to the uppercase "Function" immediately.
Please post code in a readable format with one command per line.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Jan
Jan am 16 Mai 2021
Bearbeitet: Jan am 16 Mai 2021
Matlab is case sensitive. There are no "Function", "For" and "Plot" commands. You should ge corresponding error messages.
I try t guess, how your code should be formatted:
function main
n = 10;
r0 = ones(n, 1);
for bet = 0.1:0.1:0.4
sol = solve(@(r) fun(r, n), r0);
norm(fun(sol, n))
end
r = ((1:n)-1) / (n-1);
plot(r, sol(0.4,:), 'g-*', sol(0.3,:), ...
'm-*', sol(0.2,:), 'b-*',sol(0.1,:), 'r-*');
end
end
There is an unmatched end. The loop over "bet" is useless, because the results are overwritten. It is not clear to me, what you want to achieve, so I cannot fix further errors.

Weitere Antworten (0)

Kategorien

Mehr zu Programming finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by