MATLAB_lab3
pdf
keyboard_arrow_up
School
California State University, Long Beach *
*We aren’t endorsed by this school
Course
206
Subject
Mathematics
Date
Apr 3, 2024
Type
Pages
21
Uploaded by Copper_gar
Lab 3 In Class 31-35
31. Create a 3x3 matrix A having random integer values between 1 and 5. Call the matrix A and, using MATLAB, perform the following operation. For each part explain the operation. A= round(5*rand(3,3))
A = 3×3
2 4 0
3 0 0
1 1 4
a= A.^A
a = 3×3
4 256 1
27 1 1
1 1 256
b= A.*A
b = 3×3
4 16 0
9 0 0
1 1 16
c= A*A-1
c = 3×3
15 7 -1
5 11 -1
8 7 15
d= A./A
d = 3×3
1 1 NaN
1 NaN NaN
1 1 1
e= det (A)
e = -48
f= inv(A)
f = 3×3
0 0.3333 0
0.2500 -0.1667 0
-0.0625 -0.0417 0.2500
32. The magic square is an arrangment of numbers in a square grid in such a way that the sum of the numbers in each row, and in each column, and in each diagonal is the same. MATLAB has a built-in function magic (n) that returns an n x n magic square. In a script file create a (5x5) magic square, and then test the properties of the resulting matrix by finding the sum of the elements in each row, in each column and in both diagonals. In eaech case, use MATLAB's built-in function sum. (Other functions that can be useful are diag and fliplr).
disp(
"magic matrix is"
)
1
magic matrix is
m= magic(6)
m = 6×6
35 1 6 26 19 24
3 32 7 21 23 25
31 9 2 22 27 20
8 28 33 17 10 15
30 5 34 12 14 16
4 36 29 13 18 11
column_sum= sum(m)
column_sum = 1×6
111 111 111 111 111 111
row_sum= sum(m,2)
row_sum = 6×1
111
111
111
111
111
111
diagonal_sum= sum(diag(m))
diagonal_sum = 111
flip_m= fliplr(m)
flip_m = 6×6
24 19 26 6 1 35
25 23 21 7 32 3
20 27 22 2 9 31
15 10 17 33 28 8
16 14 12 34 5 30
11 18 13 29 36 4
second_diagonal_sum= sum(diag(flip_m))
second_diagonal_sum = 111
33. Solve the following system of three linear equations:
-2x + 5y + 7z = -17.5
3x - 6y + 2z = 40.6
9x - 3y + 8z = 56.2
A= [-2 5 7; 3 -6 2; 9 -3 8]
A = 3×3
-2 5 7
3 -6 2
9 -3 8
2
B= [-17.5; 40.6; 56.2]
B = 3×1
-17.5000
40.6000
56.2000
x= linsolve(A,B)
x = 3×1
3.2000
-4.6000
1.7000
34. Solve the following system of six linear equations
2a - 4b + 5c - 3.5d + 1.8 + 4f = 52.52
-1.5a + 3b + 4c - d - 2e - 5f = -21.1
5a + b - 6c + 3d - 2e + 2f = -27.6
1.2a - 2b + 3c + 4d - e + 1.5f = 9.16
4a + b - 2c - 3d - 4e + 1.5f = -17.9
3a + b - c + 4d - 2e - 4f = -16.2
A= [2 -4 5 -3.5 1.8 4; -1.5 3 4 -1 -2 5; 5 1 -6 3 -2 2; 1.2 -2 3 4 -1 1.5; 4 1 -2 -3 -4 1.5; 3 1 -1 4 -2 -4]
A = 6×6
2.0000 -4.0000 5.0000 -3.5000 1.8000 4.0000
-1.5000 3.0000 4.0000 -1.0000 -2.0000 5.0000
5.0000 1.0000 -6.0000 3.0000 -2.0000 2.0000
1.2000 -2.0000 3.0000 4.0000 -1.0000 1.5000
4.0000 1.0000 -2.0000 -3.0000 -4.0000 1.5000
3.0000 1.0000 -1.0000 4.0000 -2.0000 -4.0000
B= [52.52; -21.1; -27.6; 9.16; -17.9; -16.2]
B = 6×1
52.5200
-21.1000
-27.6000
9.1600
-17.9000
-16.2000
x= linsolve(A,B)
x = 6×1
2.0454
-5.7877
2.6409
-1.7440
4.8091
-0.6717
3
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help
35. A football stadium has 100,000 seats. In a game with full capacity people with the following ticket and associated cost attended the game: Student: $25
Alumni: $40
Faculty: $60
Public: $70
Veterans $32
Guests: $0
Determine the number of people that attended the game in each cost category if the total revenue was $4,897,000, there were 11,000 more alumni than faculty, the number of public plus alumni together was 10 times the number of veterans, the number of faculty plus alumni together was the same as the number of students, and the number of faculty plus students together was four times larger than the number of guests and veterans together. %ticket costs
sc = 25;
ac = 40;
fc = 60;
pc = 70;
vc = 32;
gc = 0;
%total revenue and seating capacity
totalRevenue = 4897000;
seatingCapacity = 100000;
%etc
alumniMoreThanFaculty = 11000;
publicPlusAlumniEquals10TimesVeterans = 10;
facultyPlusAlumniEqualsStudents = 1;
facultyPlusStudentsEquals4TimesGuestsPlusVeterans = 4;
%equations
LHS=[1 1 1 1 1 1
25 40 60 70 32 0
0 1 -1 0 0 0
0 1 0 1 -10 0
-1 1 1 0 0 0
1 0 1 0 -4 -4]
LHS = 6×6
1 1 1 1 1 1
25 40 60 70 32 0
0 1 -1 0 0 0
0 1 0 1 -10 0
-1 1 1 0 0 0
1 0 1 0 -4 -4
4
RHS=[100000;4897000;11000;0;0;0]
RHS = 6×1
100000
4897000
11000
0
0
0
X=inv(LHS)*RHS
X = 6×1
10
4
×
2.5000
1.8000
0.7000
4.2000
0.6000
0.2000
sum(X)
ans = 1.0000e+05
Chapter 4
score1= input(
'enter your score for midterm 1'
)
score2= input(
'enter your score for midterm 2'
)
score3= 100
name= input(
'please enter your name: '
, 's'
) %string type for character
avg= (score1 + score2 + score3)/3
fprintf(
'My name is %s. My average score for the semester is %f' , name,avg) % %s is insert of string
x= 1:5 %1 through 5
x = 1×5
1 2 3 4 5
y= sqrt(x)
y = 1×5
1.0000 1.4142 1.7321 2.0000 2.2361
T= [x;y]
T = 2×5
1.0000 2.0000 3.0000 4.0000 5.0000
1.0000 1.4142 1.7321 2.0000 2.2361
%fprintf('if the number is: %i, its square root is %f\n'.T)
5
% %i insert an integer
% \n start a new row
Example 4-1
V= 200000
V = 200000
r= 30
r = 30
R= 45
R = 45
theta= asin(r/R)
theta = 0.7297
h= R-R*cos(theta)
h = 11.4590
Vcap= 1/3*pi*h^2*(3*R-h)
Vcap = 1.6988e+04
H= (V-Vcap)/(pi*r^2)
H = 64.7274
Vcyl= pi*r^2*H
Vcyl = 1.8301e+05
S= 2*pi*r*H+2*pi*R*h
S = 1.5441e+04
fprintf(
'the height of the silo is %5.2f, \n and the surface area is %i' ,H,S)
the height of the silo is 64.73, and the surface area is 1.544078e+04
%read 103-105
Create Data Files
clear;clc;
V=[3 16 -4 1.3]
6
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help
V = 1×4
3.0000 16.0000 -4.0000 1.3000
A=[6 -2.1 15.5; -6.1 8 11]
A = 2×3
6.0000 -2.1000 15.5000
-6.1000 8.0000 11.0000
save -ascii DatSavAsci2
load(
'CH4Data.txt'
)
xlsread(
'Book.xlsx'
)
ans = 3×2
56.0000 -4.2000
3.0000 7.5000
-1.6000 198.0000
Data1=xlsread(
'Book.xlsx'
)
Data1 = 3×2
56.0000 -4.2000
3.0000 7.5000
-1.6000 198.0000
table(Data1)
ans = 3×1 table
Data1
1
2
1
56
-4.2000
2
3
7.5000
3
-1.6000
198
x= Data1(:,1)
x = 3×1
56.0000
3.0000
-1.6000
y= Data1(:,2)
y = 3×1
-4.2000
7.5000
198.0000
table(x,y)
7
ans = 3×2 table
x
y
1
56
-4.2000
2
3
7.5000
3
-1.6000
198
plot(x,y,
'o'
)
plot(x,y,
'o'
)
8
Plot Specifiers
plot(x,y,
'--r'
)
9
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help
plot(x,y,
'--y'
)
10
plot(x,y,
'^r'
, LineWidth=1)
y= @ (x) x.^2+4*sin(2*x)-1
y = function_handle with value:
@(x)x.^2+4*sin(2*x)-1
%function plot
fplot(y,[1,1])
11
CW Chapter 4-3
Write a MATLAB program that determines the radius, r, of the largest sphere that can be inscribed inside a cone with base radius R and height H. For input the program asks the user to enter values for R and h. The program then calculates r rounded to the nearest tenth. For output the program displays the message: "The radius of the largest sphere that can be inscribed inside a cone with a base radius of XX in. and height of XX in., is: XX in." where XX are the corresponding numerical values. Use the program to determine r for a cone with R=8in. and h=22in.
R= input(
'Enter the base radius of the cone (in inches:'
)
h= input(
'Enter the height of the cone (in incehs):'
)
r= h/(sqrt(1 + (h/R)^2) + 1)
r_rounded= round(r*10)/10
fprintf(
'The radius of the largest sphere that can be insribed inside a cone with a base radius of %.1f in. and height %.1f in., is: %.1f in.\n'
, R, h, r_rounded)
CW Chapter 5-3
Use the plot command to plot the function:
12
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help
x= linspace(-7,7)
x = 1×100
-7.0000 -6.8586 -6.7172 -6.5758 -6.4343 -6.2929 -6.1515 -6.0101
y= 3.*cos(1.7.*x).*exp(-0.3.*x) + 2.*sin(1.4.*x).*exp(0.3.*x)
y = 1×100
19.3473 14.5161 9.2456 3.8696 -1.2992 -5.9851 -9.9617 -13.0611
plot(x,y)
xlabel(
'x'
)
ylabel(
'3cos(1.7x)exp(-0.3x)+2sin(1.4x)exp(o.3x)'
)
grid on
HW Chapter 4-11
Early explorers often estimated altitude by measuring the temperature of boiling water. Use the following two equations to make a table that modernday hikers could use for the same purpose. where p is atmospheric pressure in inches of mercury, is boiling temperature in F, and h is altitude in feet. The table should have two columns , the first altitude and the second boiling temperature. The altitude should range between -500ft and 10,000ft at increments of 500ft.
altitudes = -500:500:10000;
13
altitude_array = [];
boiling_temp_array = [];
for h = altitudes
p = 29.921 * (1 - 6.8753e-6 * h);
Tb = 49.161 * log(p) + 44.932;
altitude_array = [altitude_array, h];
boiling_temp_array = [boiling_temp_array, Tb];
end
table_data = [altitude_array', boiling_temp_array'];
disp(
'Altitude (ft) Boiling Temperature (°F)'
);
Altitude (ft) Boiling Temperature (°F)
disp(table_data)
1.0e+04 *
-0.0500 0.0212
0 0.0212
0.0500 0.0212
0.1000 0.0212
0.1500 0.0211
0.2000 0.0211
0.2500 0.0211
0.3000 0.0211
0.3500 0.0211
0.4000 0.0211
0.4500 0.0210
0.5000 0.0210
0.5500 0.0210
0.6000 0.0210
0.6500 0.0210
0.7000 0.0210
0.7500 0.0209
0.8000 0.0209
0.8500 0.0209
0.9000 0.0209
0.9500 0.0209
1.0000 0.0209
HW Chapter 4-23
A truss is a structure made of members joined at their ends. For the truss shown in the figure, the forces in the 11 members are determined by solving the following system of 11 equations:
14
Write the equations in matrix form and use MATLAB to determine the forces in the members. A positive force means tensile force and a negative force means compressive force. Display the results in a table where the first column displays the member number and the second column displays the corresponding force
A1= [cosd(28.5) 1 0 0 0 0 0];
A2= [sind(28.5) 0 0 0 0 0 0];
A3= [-cosd(28.5) 0 -cosd(58.4) 0 cosd(58.4) cosd(28.5) 0];
A4= [-sind(28.5) 0 -sind(58.4) 0 -sind(58.4) -sind(28.5) 0]
A4 = 1×7
-0.4772 0 -0.8517 0 -0.8517 -0.4772 0
A5= [0 0 0 -1 -cosd(58.4) 0 1];
A6= [0 0 0 0 0 sind(28.5) 0];
A7= [0 0 0 0 0 -cosd(28.5) 0];
A= [A1;A2;A3;A4;A5;A6;A7];
B= [3000; -6521; -3000; 0; 0; -7479; 0];
C= inv(A)*B
Warning: Matrix is singular to working precision.
C = 7×1
NaN
NaN
NaN
NaN
NaN
NaN
NaN
HW Chapter 4-27
During a golf match, a certain number of points are awarded for each eagle and a different number for each birdie. No points are awarded for par, and a certain number of points are deducted for each bogey and a different number deducted for each double bogey (or worse). The newspaper report of an important match neglected to mention what these point values were, but did provide the following table of the results: Golfer Eagles Birdies Pars Bogeys Doubles Points
A 1 2 10 1 1 5
B 2 3 11 0 1 12
C 1 4 10 1 0 11
D 1 3 10 2 0 8
15
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help
From the information in the table write four equations in terms of four unknowns. Solve the equations for the unknown points awarded for eagles and birdies and points deducted for bogeys and double bogeys.
A= [1 2 -1 -1; 2 3 0 -1; 1 4 -1 0; 1 3 -2 0]
A = 4×4
1 2 -1 -1
2 3 0 -1
1 4 -1 0
1 3 -2 0
B= [5; 12; 11; 8]
B = 4×1
5
12
11
8
Points= linsolve(A,B)
Points = 4×1
4.0000
2.0000
1.0000
2.0000
fprintf(
'Point for Eagles %d\n'
, Points(1))
Point for Eagles 4
fprintf(
'Point for Birdies %d\n'
, Points(2))
Point for Birdies 2
fprintf(
'Point for Bogeys %d\n'
, Points(3))
Point for Bogeys 1
fprintf(
'Point for Doubles %d\n'
, Points(4))
Point for Doubles 2.000000e+00
HW Chapter 5-15
The shape of the heart shown in the figure is given by the equation:
Make a plot of the heart
x= linspace(-1, 1, 1000)
x = 1×1000
-1.0000 -0.9980 -0.9960 -0.9940 -0.9920 -0.9900 -0.9880 -0.9860
16
y1= sqrt(1-x.^2) +abs(x).^(2/3)
y1 = 1×1000
1.0000 1.0619 1.0867 1.1054 1.1210 1.1345 1.1465 1.1575
y2= -sqrt(1-x.^2) +abs(x).^(2/3)
y2 = 1×1000
1.0000 0.9354 0.9079 0.8866 0.8684 0.8522 0.8374 0.8238
figure;
plot(x, y1, 'r'
, 'LineWidth'
, 2)
hold on
;
plot(x, y2, 'r'
, 'LineWidth'
, 2)
xlabel(
'x'
)
ylabel(
'y'
)
title(
'Heart Shape'
)
axis equal
grid on
;
HW Chapter 5-25
The height and speed of a projectile shoot at a speed v0 at an angle as a function of time are given by:
17
where g = 9.81m/s2 . Determine the time that the projectile will hit the ground and plot the height and the speed as a function of time (two plots on one page) for the case that v0 = 200 m/s and 0=70. Add titles and label the axes.
v_0= 200
v_0 = 200
theta= 70
theta = 70
g= 9.81
g = 9.8100
theta= deg2rad(theta)
theta = 1.2217
t_flight= (2* v_0* sin(theta)) /g
t_flight = 38.3157
t= linspace(0, t_flight, 1000)
t = 1×1000
0 0.0384 0.0767 0.1151 0.1534 0.1918 0.2301 0.2685
h= v_0* t* sin(theta) - (0.5 *g * t.^2)
h = 1×1000
10
3
×
0 0.0072 0.0144 0.0216 0.0287 0.0359 0.0430 0.0501
v= sqrt(v_0^2 -2 * v_0 *g *t *sin(theta) + g^2 *t.^2)
v = 1×1000
200.0000 199.6465 199.2930 198.9397 198.5864 198.2332 197.8801 197.5271
%Plot height vs. time
subplot(2,1,1)
plot(t,h)
title(
'Height vs. Time'
)
xlabel(
'Time(t)'
)
ylabel(
'Height(h)'
)
%Plot speed vs. time
subplot(2,1,2)
18
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help
plot(t,v)
title(
'Speed vs. Time'
)
xlabel(
'Time(t)'
)
ylabel(
'Speed(s)'
)
%Display Time of Flight
fprintf(
'Time of flight: %.2f seconds\n'
, t_flight)
Time of flight: 38.32 seconds
HW Chapter 5-37
A simply supported beam is loaded as shown. The shear force V and bending moment M as a function of x are given by the following equations:
Plot the shear force and the bending moment as a function of x (two figures on one page such that the shear force diagram is displayed above the bending moment diagram).
19
x = 0:0.01:20;
subplot(2,1,1);
plot(x,shearforce(x)), xlabel(
'x'
),ylabel(
'shear force'
)
subplot(2,1,2);
plot(x,momentum(x)), xlabel(
'x'
),ylabel(
'bending momentum'
)
function v =shearforce(x)
v = zeros(size(x));
q = x >= 0 & x <= 8;
v(q)=400-200*x(q);
q = x>=8 & x<=12;
v(q) = 0*x(q)-1200;
q = x>=12 & x<=20;
v(q) = -250*x(q) + 5000;
end
function v = momentum(x)
v = zeros(size(x));
q = x >= 0 & x <= 8;
v(q) = (-100*(x(q).^2)) + (400*x(q));
q = x>=8&x<=12;
v(q) = (-1200*x(q)) + 6400;
q = x>=12 & x<=20;
20
v(q) = (-125*(x(q)-12).^2 ) + (2000*x(q)) - 32000;
end
21
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help