How do I calculate integral from function based on ode45?

Hello,
I am working out multiple ways to calculated error between function and asymptotic value.
One of these ways is by calculating area with integral, in the code below this is named totalaverror4. However, these values differ quite from the other methods. Are there any other ways to calculate the total error of a function? totalaverror1 and totalaverro4 should give (approximately) the same answer.
%Initial values and known predetermined values
n=6; %number of agents in the system (nodes)
x0=abs(rand(n,1)); %initial values (random)
avx0=sum(x0)/n; %average of initial values, which is the to be reached consensus value
t0=0;
tf=25;
steps=10*tf+1;
tspan=linspace(t0,tf,steps); %time span
Fs=(tf-t0)/(steps-1); %stepsize
%
[t, x]=ode45(@odefunavcon,tspan,x0); %function with output time t and state x
xcon=x(end,:); %consensus value
subplot(2, 2, 1)
plot(t,x,'-o')
title('Solution of average consensus algorithm with ODE45');
xlabel('Time t');
ylabel('Solution x');
hold on
%results per step av con
x;
t;
[numrows]=size(t);
err=zeros(numrows);
averroravcon=zeros(numrows);
for i=1:numrows
for j=1:n
err(i,j)=(x(i,j)-avx0);
end
averroravcon(i)=sum(abs(err(i,:))); %averror per step of t
averroravconsr(i)=sum(sqrt(err(i,:).^2));
sosprop(i)=x(i,:)*L*(x(i,:))';
end
totalaverror1=sum(averroravcon)
totalaverror2=sum(averroravconsr)
totalaverror3=sum(sosprop)
totalaverror4=trapz(t,averroravcon)

Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Tags

Asked:

on 26 May 2020

Edited:

on 26 May 2020

Community Treasure Hunt

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

Start Hunting!