Calculating Percent Difference at specific intervals

1 Ansicht (letzte 30 Tage)
Hudson Harrell
Hudson Harrell am 19 Nov. 2020
Kommentiert: Hudson Harrell am 19 Nov. 2020
I'm trying to calculate the percent difference between the Euler Method and the Actual solution at time t=[2, 3, 4, 5] and display them. Here is my code. The code originally calculates and plots the outputs of both methods at time t=[1: 0.1 :5], but I want to know the percent difference at specific points t=[2, 3, 4, 5]. Any suggestions would be appreciated.
t0=1;
tf=5;
x0=1;
f=@(x,t) t-1+(1/t)-((2*x)/t);
h=.1;
t=t0:h:tf;
n=length(t);
%% Actual Solution
x1=@(t) (1/4)*t.^2-(1/3)*t+(1/2)+(1/12*t.^2);
plot(t,x1(t),'r*')
%% Euler Method
x(1)=(1/2);
for i=1:n-1
x(i+1)=x(i)+h*f(x(i),t(i));
B=x(i+1);
end
hold on;
plot(t,x,'LineWidth',3);

Akzeptierte Antwort

David Hill
David Hill am 19 Nov. 2020
m=zeros(1,5);
for i=2:5
a= find(t==i);
m(i)=abs(x(a)-x1(a))/mean([x(a),x1(a)]);
end

Weitere Antworten (0)

Kategorien

Mehr zu MATLAB finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by