Reference no: EM13973203
There will be two parts to the exam. A written part and a Matlab part. The written part will dictate the percentage of the Matlab part in scoring. The written part is closed book closed notes. The Matlab part is open book open notes. All the material covered in class and presented in the class notes is included in the material that will be tested. The notes are posted in Black Board under course documents.
When studying for the test understand how each and every line in the notes computes the final answer to the problem. Try and type these solutions on your own without looking at the solution. Solve additional problems from the text book on your own. Make sure you come to class regularly and do the work assigned to get the extra credit that has been offered.
Randi(10,5) generates an 2x2 matrix of random numbers from 1 to 10 challenge problem: Create a guessing game that where your group picks a number and its address in a random 5x5 matrix of integrs from 1 to 10. the program should give the correct answer and state whether the group wins or looses
Question 1:
Clear erases the memory;clc erases the screen
The '%' symbol comments out the line Notice that comments are in green 5+2 notice that the ans = 7 in the command window once the 'play' button on the top is clicked; the '+' operator adds the two numbers; the '-' operator subtracts the second 5-2 number from the first 5*2 '*' multiplies the two numbers 5/2 '/' divides the first by the second 5^2 '^' applies the second number as an exponent to the first Arithmetic operators are given preference in this order
Parenthesis are given the highest preference
(5+2)/2
5+2/2
Variable may be used where applicable in programming
Consider 5+2=7; there are three values in this equation
These are 5, 2 and 7. Each of these numbers can be represented by a variable name of your choice, eg:
x1=5 % x1 represents 5
y1=2 % y1 represents 2
z1=x1+y1 % z1 takes the value of 7
It is standard practice in programming to use variables Variables can be any name of your choice but preferably alphanumeric, say x1, y1 or z1; These variables are unique. There are standard available functions in Matlab such as Trigonometric functions
A function in matlab has a name and arugument(s); example y2=sind(30)% y2 is a variable, sind is the name of the function and 30 is the argument (within the parenthesis)
To know how to use the various functions google them clear
clc
A ';' after an expression or equation suppresses printing it can also be used to separate equations on the same line x1=3;% Notice that the ';' suppresses the printing after play
{
y1=2
z1=2*x1+3*y1
z2=2*x1/(3*y1)
z3=2*x1^2+3*y1^2
z4=(2*x1)^2+(3*y1)^2
z5=2*x1+3*y1+5*(x1+y1)/2
z6=2/x1+3/y1+x1^3
z7=5*x1/(7*y1)+2
z8=8*(3*x1+2*y1)
z9=(14*x1+17*y1)/3
z10=(5+x1)/(7*y1)+2
}
x2=[2,3,4,5];
x3=[-1,1,1,-1,-1];
y3=[-1,-1,1,1,-1];
figure(1), plot(x3,y3,-10,-10,10,10)
for i=1:10
disp('HELLO')
end
for i=-5:0.5:5
figure(1), plot(x3+i,y3+i,-10,-10,10,10)
pause(0.5)
end
Chalenge move the box on a diagonal line also, move the box on a circle of raduis 1
Do problems all the odd problems from page 33 to page 37. Home work will not be checked
But will be discussed if there are questions them in class
Question 2:
Input Segment ( This segment should have all the known values) R1=0.08206; %(Notice that these given values are inserted as T1=273.2; % variables that have been assigned these values. V1=22.41; % The assignment is done by the '=' sign)
n1=1
a1=6.49;
b1=0.0562;
Main Segment (This is the part that programs the equations)
P1=n1*R1*T1/V1;
P2=n1*R1*T1/(V1-n1*b1)-a1*n1^2/V1^2
Output Segment ( This segment contains the results)
disp('This is the value of the pressure using P = nRT/V')
P1
Finding the roots of a polynomial
Find the roots of the polynomial y=x^2+3x+2
The polynomial is entered as: y=[1,3,2] % Notice thst 1,3,2 are the coefficients of Roots_y=roots(y) % the terms in the polynomial in decending order of the power of each term
Do problem 26 on page 37
Input Segment
b1=180;
b2=165;
c1=115;
A1=120;
A2=100;
Main Segment
a2=b1^2+c1^2-2*b1*c1*cosd(A1);
p1=[1,-2*b2*cosd(A2),b2^2-a2];
c2=roots(p1);
Output Segment
for i=1:2
if c2(i)>0
disp(' ')
disp(['The value of c2 is:',num2str(c2(i))])
else
% disp(' ')
% disp([num2str(c2(i)), 'is not the value of c2:'])
end
end
% Predefined constants
pi
exp(1)
1i
Functions are predefined (an interesting one is sound) y2=10*cosd([0:72000]);
sound(y2,110000)
Challenge question use a for loop to create the sound of a motor cycle and use it with the box that went on the track
Question 3:
One and Two Dimensional Arrays
A one dimensional array appears as: x1 = [1,2,5,7,3];
In this case x1 is the name of the variable 1,2,5,7,3 are the values assigned to x1 The value 7 can be used in Matlab by tying x1(4)
Two dimensional arrays consist of more than one
One Dimensional array; such as: x2 = [1,2,4,6,1;2,3,1,5,9]% The semi colon after the '1' moves the 2,3,1,5,9 to the next row below
In this case x2 is the name of the variable
The value 3 can be used in Matlab by typing?
x2(2,2)% The (2,2) stands for second row second column
Arrays can be created by three methods a) Tying them b)Using a range variable command;c) Programming
Note b) can be used for equispaced data only Example a) x3=[2,4,7,8] Example b) x4=0:0.5:4% 0 is the starting value;4 is the ending value
and 0.5 is the step size
Example c) for i=1:4 x5(i)=i^2; end x5
Create the two dim array x6=[1,2,3;4,5,6] by methods a,b,c x6_1=[1,2,3;4,5,6] % a) x6_2=[1:1:3;4:6] % b) ; since 1 is the step size it is not required for i=1:3
x6_3(1,i)=i;
x6_3(2,i)=i+3; end
Go through the definitions in Table 2.1-1 on page 45 logspace(1,100,2) linspace(1,9,10)
Find the max value, and its address, in x6
x6=[1,10,3;4,5,6]; [x7,k1]=max(x6);
[max_x6,k2]=max(x7);
disp(['The max value is ',num2str(max_x6)])
disp(['The address is: (',num2str(k1(k2)),',',num2str(k2),')'])
To get just the max value type: max_6=max(max(x6))% This is known as nesting of functions
disp('HELLO')
Do TYU on page 47
Do problem 3 on page 96 randi(10,5)% Generates a 5X5 matrix of random numbers from 1 to 10
Challenge problem: Crete a guessing game where your group picks a number and its address in a random 2X2 matrix of integrs from 1 to 100.
The program should give the correct answer and state whether the group wins or looses
Refine your program so that the numbers are not repeated in the matrix
The input statement interactively assigns a value to a variable, ex guess=input('Enter the Guess value ')
Question 4:
Creating New Matrices from Existing Ones
A=[1,2,3,4;5,6,7,8;9,10,11,12]
If a new matrix is to be created using row 2 and 3 It can be created as:
B=[A(2:3,:)]% The 2:3, in the row address location is the range of the rows included; and the ':' includes all the column elements
Create a new matrix from A using columns 1, 2, and 3 C=[A(:,1:3)]% The ':' is for all the row elements
Create a new matrix D from A using columns 1,2,3, and 4 D=A
A=[3,7,-4,12;-5,9,10,2;6,13,8,11;15,5,4,1]
5 a) V1=A(:,2)
6 c) D1=A(1:2,2:4) Includes all the elements within row 1,2 and col 2,3,4
Matrix Operations and Operators
Addition:
Any two matrices that have number of rows and columns can be added with a '+' sign between the matrix names
Example
A2 = [1,2;3,4] Has two rows and two columns, and
B2= [5,6;7,8] Has two rows and two columns thus they can be added
C2 = A2+B2 % Each element of A2 is added to that of B2
Note A2 and B2 have the same size, ie:2 rows and 2 columns
Subtraction is done in a similar way by using '-' for '+'
Multiplication of two matrices: THERE ARE TWO TYPES
a) Element by Element (scalar), and b)Matrix multiplication
a) In element by element the two matrices (or vectors), each element from the first matrix (or vector) is multiplied by a corresponding element of the second matrix (or vector). This operation is implemented by placing a '.*' between the two matrices (or vectors);
Ex: C3=A2.*B2 % C3=[1*5,2*6;3*7,4*8]
The second type of multiplication is:
b) Matrix Multiplication: In this case, one has to ensure that the number of columns of first matrix (or vector) should equal the number of rows of the second matrix (or vector). The resultant matrix will have the dimension (number of rows of the first times nuber of columns of the second.This operation is implemented by placing a '*' between the two matrices (or vectors).
Ex: C4=A2*B2 % Observe that the number of columns of A2 = number of rows of B2; and the resultant C4 has 2 rows and 2 columns
In this case C4=[(1*5+2*7),(1*6+2*8);(3*5+4*7),(3*6+4*8)]
Verify these numbers by hand
Consider the vector Rx=[0,1]and Ry = [0,0]
Rx=[0,1];Ry=[0,0] figure(1), plot(Rx,Ry,-2,-2,2,2)
Also consider the rotational Matrix R given by:
R=[cosd(60),-sind(60);sind(60),cosd(60)]% Size equals (2X2)
Now consider the multiplication of each pair of numbers from
Rx and Ry with R as:
Temp=R*[Rx(1);Ry(1)];
Rx(1)=Temp(1);
Ry(1)=Temp(2);
Temp=R*[Rx(2);Ry(2)];
Rx(2)=Temp(1);
Ry(2)=Temp(2);
pause(1)
figure(1), plot(Rx,Ry,-2,-2,2,2)
Work these numbers by hand and verify each step above Challenge problem create a clock using this information
Question 5:
Given x = [1,2,3,4], use two methods to create y=[1,4,9,16] without typing the elements of y by hand
x=[1,2,3,4]
y1=x.^2
y2=x.*x
Create y3=[1,4,9,16], using a for loop for i=1:length(x)
y3(i)=x(i)^2;
y4(i)=x(i)*x(i);
end
y3
y4
x2=[1,2,3;4,5,6]
Create y5=[1,4,9;16,25,36] using a for loop [m,n]=size(x2); m gives the number of rows and n the number of cols for i=1:n
y5(1,i)=x2(1,i)^2;
y5(2,i)=x2(2,i)*x2(2,i);
end
y5
Work by hand by substituting each i into the equations above HW:Create y5 by using m in the for loop above Create y6=[1,4;9,16] using two for loops
for i=1:m
for j=1:n
y6(i,j)=x2(i,j)^2; % or y6(i,j)=x2(i,j)*x2(i,j)
end
end
y6
Work by hand by substituting each i and j into the equation above
Given x+2y=4 and 2x+y=5; in matrix form is expressed as:
AX=B;where A=[1,2;2,1] and X=[x;y] and B=[4;5]
The solution is given by: X=inverse of A*B, as:
A=[1,2;2,1];
B=[4;5];
X=inv(A)*B % Note:x=X(1) and y=X(2); X is not equal to x
Polynomials are defined in matlab as the vector of its coefficients
Ex: y8=x^2 + 3x + 4 is typed as:
y8=[1,3,4]
Ex: y8=x^2 + 4 is typed as:
y8=[1,0,4]
Two polynomials can be multiplied using the conv function and; divided using the deconv function (Table 2.6-1,pg 87)
Do TYU on page 85 and 88.
Do problems 16,20, 35, 55, 56 and 57, ppg 96 - 119 Exam I is next week
Attachment:- pro.rar