Calculating Percent Difference at specific intervals

1 回表示 (過去 30 日間)
Hudson Harrell
Hudson Harrell 2020 年 11 月 19 日
コメント済み: Hudson Harrell 2020 年 11 月 19 日
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
  1 件のコメント
Hudson Harrell
Hudson Harrell 2020 年 11 月 19 日
Thank you i'll try this out right now!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by