Professional Documents
Culture Documents
Q2) Write a visual basic program to input a triangle dimensions (B) and (H) then find moment of
inertia (I) using the following equation:
𝐵𝐻
𝐼=
36
Q3) Write a visual basic program to input applied force on a concrete cylinder (P) and its
diameter (D). Then calculate the compression strength (S) using the following equations:
3.14159
𝐴= 𝐷
4
𝑃
𝑆=
𝐴
Q4) Write a visual basic program to find the moment in a simply supported beam of length (L)
and subjected to concentrated midspan force (P) using the following equations:
𝑃𝑥 𝐿
𝑀= , 𝑖𝑓 𝑥 <=
2 2
𝑃(𝐿 − 𝑥) 𝐿
𝑀= , 𝑖𝑓 𝑥 >
2 2
Where 𝑥 is the distance from the support.
4 2 3
𝐴= 8 −3 2
3 1 5
1 4
𝐵= 2 5
3 6
𝐶 = 𝐵 𝐴𝐵
5 5
𝐷=
−5 0
𝐸 =𝐶+𝐷
2) 𝐹 is the determinant of 𝐸
𝐹 = |𝐸 |
3) 𝐺 given that
0.2 0.2 0.2 0.3 0 0
𝐺=𝐴 + 0.2 0.2 0.2 − 0 0.3 0
0.2 0.2 0.2 0 0 0.3
4) 𝐻 is the second row of matrix 𝐺
7) 𝑘 = 𝐽
𝐺 𝐺
9) 𝑄 =
𝐺 𝐺
b) Write MATLAB code to change the value of the following:
1) 𝐿 =7
1
3) Third column of 𝐿 equals 3
2
1
𝐿 𝐿 1 2
6) =
𝐿 𝐿 4 3
Solution:
%----------------------
A=[4 2 3; 8 -3 2; 3 1 5];
B=[1 4; 2 5; 3 6];
C=B'*A*B;
D=[5 5; -5 0];
E=C+D
%----------------------
F=det(E)
%----------------------
G=inv(A)+0.2*ones(3,3)-0.3*eye(3)
%----------------------
H=G(2,:)
%----------------------
J=G(:,[1 3])
%----------------------
[m,n]=size(J)
%----------------------
k=J(1,2)
%----------------------
L=zeros(4,7)
%----------------------
Q=G([1 3],[2 1])
%----------------------
L(1,1)=7
%----------------------
G(2,:)=[1 2 3]
%----------------------
L(:,3)=[1; 3; 2; 1]
%----------------------
B(2,:)=[]
%----------------------
A(:,end)=[]
%----------------------
L([2 3],[5 6])=[1 2; 4 3]
Plot Examples
1) Write MATLAB code to plot the following function:
𝑥3
𝑦= − 2𝑥 2 + 𝑥 − 4
5
−5 ≤ 𝑥 ≤ 5, ∆𝑥 = 0.01
Solution:
x=[-5:0.01:5];
y=x.^3/5-2*x.^2+x-4;
figure;
plot(x,y,'r');
xlabel('x');
ylabel('y');
title('Polynomial');
2) Write MATLAB code to plot the following functions:
𝑦1 = sin(𝑥 )
𝑦2 = cos(𝑥 )
𝜋
−2𝜋 ≤ 𝑥 ≤ 2𝜋, ∆𝑥 =
20
Solution:
x=[-2*pi:pi/20:2*pi];
y1=sin(x);
y2=cos(x);
figure;
plot(x,y1,'b-*',x,y2,'go');
xlabel('x');
ylabel('y1, y2');
title(' Trigonometric');
3) Write MATLAB code to plot the following functions:
𝑦1 = e−0.2t
𝑦2 = e−0.2t cos(3𝑡)
0 ≤ 𝑡 ≤ 10, ∆𝑡 = 0.1
Solution:
t=[0:0.1:10];
y1=exp(-0.2*t);
y2= exp(-0.2*t).*cos(3*t);
figure;
plot(t,y1,'k-.',t,y2,'m--');
xlabel('t');
ylabel('y1, y2');
title(' Exponential Decay');
legend('y1','y2')
Plot Options
Color Marker Shape Line Type
_____________________________________________
b blue . point - solid
g green o circle : dotted
r red x x-mark -. dashdot
c cyan + plus -- dashed
m magenta * star (none) no line
y yellow s square
k black d diamond
w white v triangle (down)
^ triangle (up)
< triangle (left)
> triangle (right)
p pentagram
h hexagram
Solving Equations Examples
1) Write MATLAB code to find the solution of the following simultaneous
linear equations:
5𝑥1 − 3𝑥2 + 2𝑥3 = 4
−3𝑥1 + 2𝑥2 − 4𝑥3 = 0
2𝑥1 − 𝑥2 − 2 = 0
Solution 1:
A=[5 -3 2; -3 2 -4; 2 -1 0]
B=[4; 0; 2]
X=inv(A)*B
Solution 2:
syms x1 x2 x3
X=solve([5*x1-3*x2+2*x3==4,-3*x1+2*x2-4*x3==0,2*x1-x2-2==0],[x1 x2 x3]);
x1Sol=X.x1
x2Sol=X.x2
x3Sol=X.x3
2) Write MATLAB code to find the solution of the following polynomial
equation:
𝑥 4 − 7𝑥 3 + 7𝑥 2 + 35𝑥 − 60 = 0
Solution 1:
syms x
X=solve(x^4-7*x^3+7*x^2+35*x-60==0,x,'MaxDegree',4)
Solution 2:
X=roots([1 -7 7 35 -60])
Calculus Examples
1) Write MATLAB code to find the derivative of the following function:
y = x 2 + xsin(x) + 5 tan(x) , x=2
Solution:
syms x
y=x^2+x*sin(x)+5*tan(x)
g=diff(y)
h=double(subs(g,2))