Calculating Percent Difference at specific intervals

1 vue (au cours des 30 derniers jours)
Hudson Harrell
Hudson Harrell le 19 Nov 2020
Commenté : Hudson Harrell le 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);

Réponse acceptée

David Hill
David Hill le 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

Plus de réponses (0)

Catégories

En savoir plus sur MATLAB dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by