Simulation Lab Unit I Solution
Simulation Lab Unit I Solution
Simulation Lab Unit I Solution
UNIT I SOLUTIONS
SIMULATION LAB
UNIT I SOLUTIONS
2. Let x = [2 5 1 6].
a. Add 16 to each element
b. Add 3 to just the odd-index elements
c. Compute the square root of each element
d. Compute the square of each element
Ans: x = [2 5 1 6]
a = x + 16
b = x(1:2:end) + 3
c = sqrt(x) or c = x.^(0.5)
d = x.^2 or d = x.*x
4. Evaluate the following MATLAB expressions by hand and use MATLAB to check
the answers
a. 2 / 2 * 3
b. 6 - 2 / 5 + 7 ^ 2 - 1
c. 10 / 2 \ 5 - 3 + 2 * 4
d. 3 ^ 2 / 4
e. 3 ^ 2 ^ 2
f. 2 + round(6 / 9 + 3 * 2) / 2 - 3
g. 2 + floor(6 / 9 + 3 * 2) / 2 - 3
h. 2 + ceil(6 / 9 + 3 * 2) / 2 - 3
5. if n > 1
m = n+1
else
m=n-1
end
Find the value of m when
a. n = 7 m = ?
b. n = 0 m = ?
c. n = -10 m = ?
Ans n = 7 gives m = 8
n = 9 gives m = -1
n = -10 gives m = -11
6. Find the solution (i.e. find 𝑥1,2) to the following equations using Matlab:
𝑥1 + 2𝑥2 = 5
3𝑥1 + 4𝑥2 = 6
Solution:
We get:
𝐴𝑥 = 𝑏
𝑥 = 𝐴-1𝑏
MathScript code:
A = [1 2; 3 4];
b = [5;6];
x = inv(A)*b
A more general approach would be to use the built--‐in backslash “\” operator to solve the
equations:
A = [1 2; 3 4];
b = [5;6];
x = A\b
This approach is more robust, because it will work even when the 𝐴 matrix is not
quadratic (meaning it is not invertible).
7. Plot 𝑠𝑖(𝜃) for 0 ≤ 𝜃 ≤ 2𝜋 using Matlab. Plot 𝑐𝑜(𝜃) for 0 ≤ 𝜃 ≤ 2𝜋 using Matlab. Try to plot
them in 2 different plots and then try to plot them in the Same plot. Finally, try to plot
them in 2 different subplots.
Solution:
Note! We cannot use Greek letters in MathScript, but use, e.g., “theta”, “x” or another name for
Your variable.
Sinus:
MathScript Code:
x = 0:0.01:2*pi;
plot(x, sin(x))
Cosinus:
x = 0:0.01:2*pi;
plot(x, cos(x))
x = 0:0.01:2*pi;
figure(1)
plot(x, sin(x))
figure(2)
plot(x, cos(x))
x = 0:0.01:2*pi;
plot(x, sin(x))
hold on
plot(x, cos(x))
or like this:
x = 0:0.01:2*pi;
plot(x, sin(x), x, cos(x))
Colors:
We can also use different colors:
clf
x = 0:0.01:2*pi;
plot(x, sin(x), 'g')
hold on
plot(x, cos(x), 'r')
Subplots:
clf
x = 0:0.01:2*pi;
subplot(2,1,1)
plot(x, sin(x), 'g')
subplot(2,1,2)
plot(x, cos(x), 'r')
Solution:
We define a script (m--‐file):
%Define Variabes
T=5;
a=-1/T;
%Start Condition, etc
x0=1;
t=[0:1:25]
%Define the function
x=exp(a*t)*x0;
%Plotting
plot(t,x);
grid
title('simulation of x'' = ax')
xlabel('time')
ylabel('x')
The Result:
Solution:
The function becomes (filename: fahrenheit.m):
function Tf = fahrenheit(Tc)
Tf = (9/5)*Tc + 32;
We test the function (you can use the command window for this):
Tc = 23
Tf = fahrenheit(Tc)
Solution:
MathScript Code:
function myplot(a)
x = 0:0.1:2*pi;
if a == 1
y=sin(x);
elseif a == 2;
y=cos(x);
elseif a == 3
y=tan(x);
else
y=x;
end
plot(x,y)
a=1;
myplot(a)
When,
a=2;
myplot(a)
11. Plot the polynomial 4 x3−3x using the function polyval. First find out how to use polyval
using the help.
Solution:
doc polyval
p = [4 0 -3 0]
y1 = polyval(p,x);
hold on
plot(x,y1,'g')
12. Create a function that finds the surface area of a cylinder based on the
Height (ℎ) and the radius (𝑟) of the cylinder.
Solution:
We have that:
function A = cylindar_surface(h,r)
% This function calculates the surface of a sylindar
A = 2*pi*r^2 + 2*pi*r*h;
>> h=8
>> r=3
>> cylindar_surface(h,r)
ans =
207.3451
Given
𝑎 = 1, 𝑏 = 3, 𝑐 = 5
Find 𝑓 (9)
(The answer should be 𝑓 (9) = 0.0044)
Tip!
You should split the expressions into different parts, such as:
poly= 𝑎𝑥! + 𝑏𝑥 + 𝑐
num =…
den=….
f=…
This makes the expression simpler to read and understand, and you Minimize the
risk of making an error while typing the expression in MATLAB.
Solution:
The MathScript Script can be written like this:
>> advexpression
f =
0.0044
14. Plot projectile trajectories using equations for ideal projectile motion:
Where y (t) is the vertical distance and x (t) is the horizontal distance travelled by the
projectile in metres, g is the acceleration due to Earth's gravity = 9.8 m/s2 and t is time
in seconds. Let us assume that the initial velocity of the projectile v0 = 50.75 m/s and
5𝜋
the projectile's launching angle 𝜃𝑜 = radians. The initial vertical and horizontal
12
positions of the projectile are given by y0 = 0 m and x0 = 0 m. Let us now plot y vs. t and
x vs. t in two separate graphs with the vector: t=0:0.1:10 representing time in seconds.
Give appropriate titles to the graphs and label the axes. Make sure the grid lines are
visible.
Solution:
b. Compute the integral, and the first and second derivatives of the above function
symbolically.
Solution:
a)
>> syms x
>> f = x^2*exp(x) - 5*x^3;
b)
>> int(f)
ans =
x^2*exp(x)-2*x*exp(x)+2*exp(x)-5/4*x^4
>> diff(f)
ans =
2*x*exp(x)+x^2*exp(x)-15*x^2
>> diff(f,2)
ans =
2*exp(x)+4*x*exp(x)+x^2*exp(x)-30*x
16. Computation with multiple anonymous functions: Create three anonymous functions
corresponding to the following expressions:
a) f(x)=x4-8x3+17x2-4x-20
b) g(x)=x2-4x+4
c) h(x)=x2-4x-5
Evaluate the following
i. f(x)-g(x)h(x) at x=3
ii. f(x)-g(x) h(x) at x=[1 2 3 4 5]
iii. f(x)/g(x)-h(x) for any x
iv. plot f(x) and f(x)/g(x)h(x) over x:[-5,5]
17. Write a for loop to compute the sum of the squares of all integers from 2 to 20.
18. Suppose that the squares of all positive odd integers were to be added up until this sum
first equals or exceeds 100. How many terms would be added? What is the final sum?