Calculating Percent Difference at specific intervals

1 次查看(过去 30 天)
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);

采纳的回答

David Hill
David Hill 2020-11-19
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by