skip to primary navigationskip to content
 

practicalOneCode.m

Objective-C source code icon practicalOneCode.m — Objective-C source code, 1 KB

File contents

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Model answers to exercises in Matlab Practical %
% Nik Cunniffe: Oct 2010                         %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Exercise One
x=[0:0.1:5];
x(10)

% Exercise Two
x = [0:0.01:2*pi];
z = cos(x);
plot(x,z,'g');

% Exercise Three
x = [0:0.01:5];
y = x.*exp(-x);
plot(x,y);

% Exercise Five
x = [0:0.2:2.0];
y = x.*exp(-x);
plot(x,y);
estimatedArea = 0.1 * (y(1) + 2*sum(y(2:10)) + y(11))

% Exercise Six
exactArea(2)

% Exercise Seven
format long
myTrap(2,10)
myTrap(2,100)

% Exercise Eight
for i=1:14
    N(i) = power(2,i);
    myErr(i) = abs(myTrap(2,N(i))-exactArea(2));
    sDisp = sprintf('%d -> %.10f\n', N(i), myErr(i))
end
% the error goes down with the step sized squared

% Exercise Nine
N = 200;
a = 2;
x = [0:a/N:a];
y = x.*exp(-x);
(a/N)*trapz(y)
myTrap(a,N)

% Exercise Ten