You are on page 1of 2

M-files from Exercise 2

1.)
function A = make_A(a,b,c,d)
A=zeros(2,2);
A(1,1) = a;
A(1,2) = b;
A(2,1) = c;
A(2,2) = d;

% Initialize A to all zeros

2.)
function y = funplots(x)
y = sin(x); % Find sin(x)
[x,y]
% Print table to the screen
% x was supposed to be a column..
plot(x,y,'r--') % Plot your answer
xlabel('x')
ylabel('sin(x)')

3.)

function y = funky(x)
y = sqrt((x-2).^2)/pi.*sin(pi*x); % Computations
[x,y]

% print table to the screen

4.)
% Create an 8x8 matrix of my choice
A = [1 2 3 4 5 6 7 8;
2 4 6 8 10 12 14 16;
3 6 9 12 15 18 21 24;
4 8 12 16 20 24 28 32;
5 10 15 20 25 30 35 40;
6 12 18 24 30 36 42 48;
7 14 21 28 35 42 49 54;
8 16 24 32 40 48 56 64];
%
y
z
x

Set y equal to the first 3 components of row 2 of A


= A(2,1:3);
= A(7,1:3);
= y + z

5.) Make the above file into a function m-file with A as the input.

M-Files from Exercise 3


1.)
function a = ave(x)
n = length(x);
sum = 0;

% find the length of x


% initialize the sum

for i=1:n
sum = sum + x(i); % add up all the components of x
end
a = sum/n; % find the average

2.)

function s = std_dev(x)
n = length(x); % Find the length of x
a = ave(x);
% find the average
sum = 0;
% Initialize sum
for i=1:n
sum = (x(i)-a)^2;
end
s = (1/(n-1))*sum;

3.) I skipped this onethe idea is implemented in (5.)


4.) and 5.)
function [d,i] = double_x(x)
d = x; % initialize variable
i=0;
% Initialize counter
while(d < 2*x)
d = d +2
i=i+1
end
6.) Involves using an if-else loop

You might also like