Ratio of data values

3 views (last 30 days)
Justin Feick
Justin Feick on 15 Feb 2019
I need to have a ratio for my errors. so the first ratio in the row should be 0, then the next one should be .080081/0.689885 to get the ratio for row 2, and the the next row .008140/.080081 and so on for the next value.
clc;
clear all;
format long
f=@(t,y)2*y;
a=0; %lower limit
b=0.5; %upper limit
disp('----------------------------------------------------------------------------') %display
disp( ' N aprroximation Erorr Ratio ')
disp('----------------------------------------------------------------------------')%display
N=5; % number of intervals
while(N<=5000)
h=(b-a)/N; %step length
t=a:h:b;
y_eu(1)=3; % intial conditon
%Euler formula
for i=1:N
y_eu(i+1)=y_eu(i)+h*f(t(i),y_eu(i));
end
%Exact solution
for i=1:N+1
y_ex(i)=3*exp(2*t(i));
end
erorr=(abs(y_eu(end)-y_ex(end)));
ratio=1;
fprintf('%0.4d \t%20f \t%20f\t%20f\n',N,y_eu(end),erorr,ratio)%display of graph
N=N*10;
end
plot(t,y_eu)
hold on
plot(t,y_ex,'r')

Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!