Computing error and plotting its graph

38 views (last 30 days)
Mathew Aibinu
Mathew Aibinu on 18 Nov 2024 at 1:07
Commented: Star Strider on 18 Nov 2024 at 20:51
%This compares the exact and approximate analytical solutions of the logistic
%model
clear all; close all;
xx=1.0;
%%main loop
n=1;
er=1;
c=2; r=0.01; K=10.4; t=0:1:50;
tol = 1e-4;
while (er > tol)
%%parameters
%%sequence
x1=c + (2*r)/sqrt(pi)*(c-c.^2/K)*t.^(1/2)...
+ r.^2*(1-2*c/K)*(c-c.^2/K)*t-(16*r.^3/(3*K*(pi).^(3/2)))*(c-c.^2/K).^2*t.^(3/2)...
+4*r.^3/(3*sqrt(pi))*(1-2*c/K).^2*(c-c.^2/K)*t.^(3/2)...
-2*r.^4/(K*pi)*(1-2*c/K)*(c-c.^2/K).^2*(t.^2)...
-3*r.^4/(2*K)*(1-2*c/K)*(c-c.^2/K).^2*(t.^2)...
+1024*r.^5/(45*K.^2*(pi).^(5/2))*(c-c.^2/K).^3*t.^(5/2)...
-16*r.^5/(15*K*sqrt(pi))*(1-2*c/K).^2*(c-c.^2/K).^2*t.^(5/2)...
+10*r.^6/(3*K.^2*pi)*(1-2*c/K)*(c-c.^2/K).^3*(t.^3)...
-8192*r.^7/(315*K.^3*(pi).^(7/2))*(c-c^2/K).^4*t.^(7/2);
%%errors
error(n)=abs(x1-xx);
er=error(n);
%%update
n=n+1;
xx=x1;
end
n=1: numel(error);
%%the following are essential as well, the plot function, paper position,
%%paper size, legend, title and save as.
grid on
plot(n,error, '-*'); %%this is the plot function, it plots the number of iteration against the errors
xlabel('Iteration numbers (n)'); %%this is the label of the x-axis.
ylabel('abs(Error)'); %%this is the label of the y-axis.
legend('graphing mann iteration'); %%this is displayed on the graph page.
title('Absolute error'); %%this is the title of the graph.

Answers (1)

Star Strider
Star Strider on 18 Nov 2024 at 1:22
The reason the code fails is that:
abs(x1-xx)
is a (1 x 51) vector (because ‘x1’ is also a (1 x 51) vector), and it’s not possible to assign a vector to a single array subscript location (although this wiill work for cell arrays). This occurs when ‘n=1’ so iit’s inherent in the code.
What value of the difference vector do you want to assign to ‘error(n)’?
%This compares the exact and approximate analytical solutions of the logistic
%model
% clear all; close all;
xx=1.0;
%%main loop
n=1;
er=1;
c=2; r=0.01; K=10.4; t=0:1:50;
tol = 1e-4;
while (er > tol)
%%parameters
%%sequence
x1=c + (2*r)/sqrt(pi)*(c-c.^2/K)*t.^(1/2)...
+ r.^2*(1-2*c/K)*(c-c.^2/K)*t-(16*r.^3/(3*K*(pi).^(3/2)))*(c-c.^2/K).^2*t.^(3/2)...
+4*r.^3/(3*sqrt(pi))*(1-2*c/K).^2*(c-c.^2/K)*t.^(3/2)...
-2*r.^4/(K*pi)*(1-2*c/K)*(c-c.^2/K).^2*(t.^2)...
-3*r.^4/(2*K)*(1-2*c/K)*(c-c.^2/K).^2*(t.^2)...
+1024*r.^5/(45*K.^2*(pi).^(5/2))*(c-c.^2/K).^3*t.^(5/2)...
-16*r.^5/(15*K*sqrt(pi))*(1-2*c/K).^2*(c-c.^2/K).^2*t.^(5/2)...
+10*r.^6/(3*K.^2*pi)*(1-2*c/K)*(c-c.^2/K).^3*(t.^3)...
-8192*r.^7/(315*K.^3*(pi).^(7/2))*(c-c^2/K).^4*t.^(7/2);
%%errors
error(n)=abs(x1-xx)
er=error(n);
%%update
n=n+1;
xx=x1;
end
Unable to perform assignment because the indices on the left side are not compatible with the size of the right side.
n=1: numel(error);
%%the following are essential as well, the plot function, paper position,
%%paper size, legend, title and save as.
grid on
plot(n,error, '-*'); %%this is the plot function, it plots the number of iteration against the errors
xlabel('Iteration numbers (n)'); %%this is the label of the x-axis.
ylabel('abs(Error)'); %%this is the label of the y-axis.
legend('graphing mann iteration'); %%this is displayed on the graph page.
title('Absolute error'); %%this is the title of the graph.
.
  2 Comments
Mathew Aibinu
Mathew Aibinu on 18 Nov 2024 at 19:30
Hi Star Strider,
Kindly assign a suitable value for the difference vector ‘error(n)’ that will make the code to work.
Star Strider
Star Strider on 18 Nov 2024 at 20:51
O.K.
It would help to know what you’re estimating. For the first approach, use the miinimum of the vector.
Try this —
%model
clear all; close all;
xx=1.0;
%%main loop
n=1;
er=1;
c=2; r=0.01; K=10.4; t=0:1:50;
tol = 1e-4;
% while (er > tol)
while (er > tol) & (n <= 100) % ADDEED FAIL-SAFE TO CHEECK RESULTS
%%parameters
%%sequence
x1=c + (2*r)/sqrt(pi)*(c-c.^2/K)*t.^(1/2)...
+ r.^2*(1-2*c/K)*(c-c.^2/K)*t-(16*r.^3/(3*K*(pi).^(3/2)))*(c-c.^2/K).^2*t.^(3/2)...
+4*r.^3/(3*sqrt(pi))*(1-2*c/K).^2*(c-c.^2/K)*t.^(3/2)...
-2*r.^4/(K*pi)*(1-2*c/K)*(c-c.^2/K).^2*(t.^2)...
-3*r.^4/(2*K)*(1-2*c/K)*(c-c.^2/K).^2*(t.^2)...
+1024*r.^5/(45*K.^2*(pi).^(5/2))*(c-c.^2/K).^3*t.^(5/2)...
-16*r.^5/(15*K*sqrt(pi))*(1-2*c/K).^2*(c-c.^2/K).^2*t.^(5/2)...
+10*r.^6/(3*K.^2*pi)*(1-2*c/K)*(c-c.^2/K).^3*(t.^3)...
-8192*r.^7/(315*K.^3*(pi).^(7/2))*(c-c^2/K).^4*t.^(7/2);
%%errors
error(n)=min(abs(x1-xx));
er=error(n);
%%update
n=n+1;
xx=x1;
x1v(n) = min(x1);
end
n=1: numel(error);
nv = 1:numel(x1v);
%%the following are essential as well, the plot function, paper position,
%%paper size, legend, title and save as.
figure
grid on
plot(n,error, '-*'); %%this is the plot function, it plots the number of iteration against the errors
xlabel('Iteration numbers (n)'); %%this is the label of the x-axis.
ylabel('abs(Error)'); %%this is the label of the y-axis.
legend('graphing mann iteration'); %%this is displayed on the graph page.
title('Absolute error'); %%this is the title of the graph.
figure
plot(nv, x1v)
grid
xlabel('n')
ylabel('x_1')
title('Functon Value vs. Iteration Number')
I can’t get this to run here for some reason. I ran it in MATLAB Online and it ran without error.
I’m not sure that it gives any useful iinformation though, since it almost immediately converges. I experimented with other functions (max, median, mean) with simiilar results.
.

Sign in to comment.

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Tags

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!