How do I type the below pseudocode in MATLAB program First i
How do I type the below pseudocode in MATLAB?
program First
integer i, imin, n <-- 30
real error, y, x <-- 0.5, h <--1, emin <--1
for i = 1 to n
h <-- 0.25h
y <-- [sin(x+h) - y]/h
error <-- |cos(x) -y|;
output i, h, y, error
if error < emin then
emin <-- error, imin <-- 1
end if
end for
output imin, emin
end program First
Solution
sims i;
sims imin;
sims error;
sims y;
x=0.5;
n=30;
h=1;
emin=1;
for i = 1 : n
h = 0.25*h;
y = (sin(x+h) - y)/h;
error = abs(cos(x) -y);
fprintf(\'%d\ %d\ %d\ %d\ \',i,h,y,error);
if error < emin
emin= error;
imin =1;
end
end
fprintf(\'%d\ %d\ \',imin,emin);
