Please help me to find the errors in my code for predictor corrector method.
Show older comments
I have written the code but it is unable to produce correct result. Can anybody help me to find the errors in the above code?
1 Comment
No obvious error in your output? I do not see any error message ?
As outside observers who have not been given documentation, we must assume that this is what your code was designed to calculate.
Otherwise we have to start making random guesses like suggesting that your r should be different, or that your f3 function should involve x4 instead of x3... because how would we know?
filename = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/822230/Untitled.m';
S = urlread(filename)
eval(S)
Answers (1)
I do not know, what you consider as "correct" result. All I see is the running code and there is no chance to predict, what you wnt to do instead.
But at least I can simplify your code, such that a debugging is much easier. Instead of repeating any equation 5 times, I've converted [f1,f2, f3, f4, f5] to one function f and did the same for the 5 inputs:
function Untitled2
lambda=0;a1=0.3;a2=0.4;r=0.23;K=0.3;phi=0.25;b=0.45;mu=0.12;
beta=1;h=0.02;
N = 5;
tfinal = 100;
t = 0:tfinal/N:tfinal;
f = @(x) [ ...
lambda - a1 * x(1) * x(2) - a2 * x(1) * x(3) + r * x(4); ...
a1 * x(1) * x(2) + a2 * x(1) * x(3) - K * x(2) - phi * x(2); ...
K * x(2) - (b + mu) * x(3); ...
b * x(3) + phi * x(2) - r * x(4); ...
mu * x(3)];
S = zeros(5, N+1);
Sp = zeros(5, N+1);
S0 = [100; 10; 3; 0; 0];
Sp(:, 1) = S0 + h^beta * f(S0) / gamma(beta) * beta;
S(:, 1) = S0 + h^beta * f(Sp) / gamma(beta+2) + h^beta * beta *...
f(S0) / gamma(beta+2);
for n=1:N
M = ((n+2)^(beta+1)+n^(beta+1)-(2*(n+1))^(beta+1)) * f(S0);
L = ((n+1)^beta - n^beta) * f(S0);
for j=1:n
M = M + ((n-j+2)^(beta+1) + (n-j)^(beta+1) - 2*(n-j+1)^(beta+1)) * ...
f(S(:, j));
L = L + ((n+1-j)^beta - (n-j)^beta) * f(S(:, j));
end
Sp(:, n+1) = S0 + L / gamma(beta);
S(:, n+1) = S0 + h^(beta) * (f(Sp(:, n+1)) + M) / gamma(beta+2);
end
Y1 = [S0, S(:, 1:N)];
plot(t, Y1)
end
Adding spaces around the operators improve the readability.
It looks strange, that you calculate S(:, 6), but do not use it in the output.
Categories
Find more on Binomial Distribution in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!