Calculating Percent Difference at specific intervals
조회 수: 2 (최근 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);
댓글 수: 0
채택된 답변
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 Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!