%MAT 275 MATLAB Assignment 275
Experiment #1
A=[1 4 2; 2 5 8; 3 6 9] % using the ';' changes to a new row
A =
1 4 2
2 5 8
3 6 9
B=[1 2 3; 4 5 6; 7 8 9]
B =
1 2 3
4 5 6
7 8 9
b=[4;23;27]
b =
4
23
27
c=[4 3 2]
c =
4 3 2
d=[1;2;3]
d =
1
2
3
A*B %multiplying the matrices A and B using '*'
ans =
31 38 45
78 93 108
90 108 126
B*A
ans =
14 32 45
32 77 102
50 122 159
c*B
ans =
30 39 48
A*d
ans =
15
36
42
C=[A B] % This combines [A] and [B]
C =
1 4 2 1 2 3
2 5 8 4 5 6
3 6 9 7 8 9
D=[B;c]
D =
1 2 3
4 5 6
7 8 9
4 3 2
x=b\A % You're going to solve for x from the given equation to make this work, also make sure
to use a 'backslash'
A(2,3)
x =
0.1028 0.2300 0.3414
ans =
8
A(2,3)=0 %Replace a value in A by singling out the (row, column) and then setting = to 0
A =
1 4 2
2 5 0
3 6 9
A(3,:)% Use this format meaning, you want to extract row 3 entirely
ans =
3 6 9
B(3,:)=[] %This calls out row 3 in vector B and then replaces it with an empty vector.
B =
1 2 3
4 5 6
Experiment #2
%Part A
type('Func2')
function [ y ] = Func2( a,r,n )
%This is a geometric function
y=0;
for i=0:n-1
y=y+a.*r.^i;
end
end
Func2 (3, 1/2, 10) %Plug in given values
ans =
5.9941
%Part B
type ('Func2b')
function [ y ] = Func2b( a,r,n )
e=0:n-1;
R=r.^e;
Y=sum(a*R)
End
Func2b(3,.5,10)
ans =
5.9941
Experiment #3
%Part A
type('Func3')
function [ q ] = Func3 %No input arguments needed
q=1
for u=1:2:19
q=q*u
end
end
Func3
q =
1
q =
1
q =
3
q =
15
q =
105
q =
945
q =
10395
q =
135135
q =
2027025
q =
34459425
q =
654729075
ans =
654729075
%Part B
u=1:2:19 %Gives all the odd numbers from 1 to 19, using a spacing of 2
u =
1 3 5 7 9 11 13 15 17 19
prod(u) %Gives you the product of all the elements in u
ans =
654729075 %Prod(u) gives you the product of all of the numbers in u
type ('Script4')
v=[]; %Sets up a vector
l=1; %Starting value for function
value=2;
while value < 1000
v=[v,value];
l=l+1;
value=2^l;
end
disp(v)
Script4
2 4 8 16 32 64 128 256 512 %Typing Script4 displays the answer in vector form
Experiment #5
type ('Func5')
function y=Func5(x)
%This is a piecewise function of x
if x==10
display('y UND at x=10')
elseif x<=3 %Elseif command changes the function to a different command for given parameters
y=x^2+1;
elseif (x>3) && (x<=5)
y=exp(x);
else
y=x/(x-10);
end
end
Func5(1) %Given value to test
ans =
2
Func5(4) %Given value to test
ans =
54.5982
Func5(7) %Given value to test
ans =
-2.3333
Func5(10) %Given value should be undefined, per the function saying is x=10, then y is undefined
y UND at x=10
Powered by TCPDF (www.tcpdf.org)