MATLAB: How to make graph

graphMATLAB

%can anyone help me how to make graph
clear all
close all
clc
w0 = 0;
h = 0.2;
disp('-------------------------------------------------------------------------');
disp(' Time(s) | w-Analytical(rad/s) | w-Euler(rad/s) | Et | et(%) ');
disp('-------------------------------------------------------------------------');
for t=0:h:0.8
wexact = (1000/3)-(1000/3)*exp(-3*t);
if t>0.1
fn = 1000 - 3*w;
wn = w+fn*h;
Et=wexact-wn;
et=abs((wexact-wn)/wexact)*100;
fprintf(' %.1f | %.2f | %.1f | %.4f | %.4f\n',t,wexact,wn,Et,et)
else
wn = w0;
Et=wexact-wn;
et=abs((wexact-wn)/wexact)*100;
fprintf(' %.1f | %.2f | %.1f | %.1f | %.4f\n',t,wexact,wn,Et,et)
end
if t>0.1
w=wn;
else
w=w0;
end
end
disp('-------------------------------------------------------------------------');
plot(t,wexact,t,wn);
legend('Exact','Approximate');
title('Graph of Concentration of Salt across Time') %Title of the graph
xlabel('Time (min)') %x-axis of the graph
ylabel('Concentration of Salt (g/L)') %y-axis of the graph
grid on; %grid graph

Best Answer

t_values = 0:h:0.8;
wexact_values = zeros(size(t_values));
for t_idx = 1 : length(t_values);
t = t_values(t_idx);
wexact = (1000/3)-(1000/3)*exp(-3*t);
wexact_values(t_idx) = wexact;
if t>0.1
fn = 1000 - 3*w;
wn = w+fn*h;
Et=wexact-wn;
et=abs((wexact-wn)/wexact)*100;
fprintf(' %.1f | %.2f | %.1f | %.4f | %.4f\n',t,wexact,wn,Et,et)
else
wn = w0;
Et=wexact-wn;
et=abs((wexact-wn)/wexact)*100;
fprintf(' %.1f | %.2f | %.1f | %.1f | %.4f\n',t,wexact,wn,Et,et)
end
if t>0.1
w=wn;
else
w=w0;
end
end
plot(t_values, wexact_values)