Simulation Lab Unit I Solution

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

SIMULATION LAB 1

UNIT I SOLUTIONS

SIMULATION LAB
UNIT I SOLUTIONS

Mr.Prashanth N, Asst Prof | Dept of Mechanical Engg, NMIT


[Type text] [Type text]
SIMULATION LAB 2
UNIT I SOLUTIONS

1. Create a vector of the even whole numbers between 31 and 75.


Ans: x = 32:2:75

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

3. Let x = [3 2 6 8]' and y = [4 1 3 5]'


a. Add the sum of the elements in x to y
b. Raise each element of x to the power specified by the corresponding
c. element in y.
d. Divide each element of y by the corresponding element in x
e. Multiply each element in x by the corresponding element in y, calling
f. the result "z".
g. Add up the elements in z and assign the result to a variable called "w".
h. Compute x'*y - w and interpret the result

Ans: x = [3 2 6 8]', y = [4 1 3 5]'


a = y + sum(x)
b = x.^y
c = y./x
z = x.*y
w = sum(z)
x'*y - w (same thing)

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 = ?

Mr.Prashanth N, Asst Prof | Dept of Mechanical Engg, NMIT


[Type text] [Type text]
SIMULATION LAB 3
UNIT I SOLUTIONS

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:
𝐴𝑥 = 𝑏

Since 𝐴 in quadratic and we can find the solution like this:

𝑥 = 𝐴-1𝑏

MathScript code:

A = [1 2; 3 4];
b = [5;6];
x = inv(A)*b

The following solution is displayed in the Output window:


𝑥1 = −4, 𝑥2 = 4.5

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.

Mr.Prashanth N, Asst Prof | Dept of Mechanical Engg, NMIT


[Type text] [Type text]
SIMULATION LAB 4
UNIT I SOLUTIONS

Sinus:
MathScript Code:

x = 0:0.01:2*pi;
plot(x, sin(x))

Cosinus:

x = 0:0.01:2*pi;
plot(x, cos(x))

Using the figure function:

x = 0:0.01:2*pi;
figure(1)
plot(x, sin(x))
figure(2)
plot(x, cos(x))

Using the hold command:


In the same plot using the hold on command:

Mr.Prashanth N, Asst Prof | Dept of Mechanical Engg, NMIT


[Type text] [Type text]
SIMULATION LAB 5
UNIT I SOLUTIONS

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')

Mr.Prashanth N, Asst Prof | Dept of Mechanical Engg, NMIT


[Type text] [Type text]
SIMULATION LAB 6
UNIT I SOLUTIONS

8. Given the following differential equation:


𝑥 = 𝑎𝑥
1
where 𝑎 = -𝑇 ,where 𝑇 is the time constant
The solution for the differential equation is:
𝑥 (𝑡) = 𝑒at𝑥o
Set 𝑇 = 5 and the initial condition (0) = 1
Plot the solution (𝑡) in the time interval 0 ≤ 𝑡 ≤ 25
Add Grid, and proper Title and Axis Labels to the plot.

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:

Mr.Prashanth N, Asst Prof | Dept of Mechanical Engg, NMIT


[Type text] [Type text]
SIMULATION LAB 7
UNIT I SOLUTIONS

9. What is the room temperature in degrees Fahrenheit?


Create a function in Script that convert a given temperature from degrees Celsius to
degrees Fahrenheit.
The conversion formula is as follows:

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)

This gives the following result:


Tf = 73.4

10. Depending on a variable 𝑎, different functions should be executed and plotted


According to:
𝑎 = 1 → 𝑦 = sin (𝑥)
𝑎 = 2 → 𝑦 = cos(𝑥)
𝑎 = 3 → 𝑦 = tan (𝑥)
𝑒𝑙𝑠𝑒 → 𝑦 = 𝑥
Create a function that plot 𝑦 based on different values for the variable 𝑎.
The 𝑥 variable can be in the range 0 ≤ 𝜃 ≤ 2𝜋

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)

Test the fnction:

a=1;
myplot(a)

Mr.Prashanth N, Asst Prof | Dept of Mechanical Engg, NMIT


[Type text] [Type text]
SIMULATION LAB 8
UNIT I SOLUTIONS

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.

Mr.Prashanth N, Asst Prof | Dept of Mechanical Engg, NMIT


[Type text] [Type text]
SIMULATION LAB 9
UNIT I SOLUTIONS

We can use the function like this:


h=8
r=3
cylindar_surface(h,r)

Solution:
We have that:

The function becomes

function A = cylindar_surface(h,r)
% This function calculates the surface of a sylindar
A = 2*pi*r^2 + 2*pi*r*h;

Testing the function:

>> h=8
>> r=3
>> cylindar_surface(h,r)
ans =
207.3451

Mr.Prashanth N, Asst Prof | Dept of Mechanical Engg, NMIT


[Type text] [Type text]
SIMULATION LAB 10
UNIT I SOLUTIONS

13. Create the following expression using MATLAB Script:

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:

% Define the variables:


a = 1;
b = 3;
c = 5;
x = 9;
% Split functions into parts for easy maintenance
poly = a*x^2 + b*x + c;
num = log(poly)-sin(poly);
den = 4*pi*x^2 +cos(x-2)*poly;
f = num/den

Testing the Script:

>> advexpression
f =
0.0044

14. Plot projectile trajectories using equations for ideal projectile motion:

Mr.Prashanth N, Asst Prof | Dept of Mechanical Engg, NMIT


[Type text] [Type text]
SIMULATION LAB 11
UNIT I SOLUTIONS

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:

We _rst plot x and y in separate _gures:


>> t = 0 : 0.1 : 10;
>> g = 9.8;
>> v0 = 50.75;
>> theta0 = 5*pi/12;
>> y0 = 0;
>> x0 = 0;
>> y = y0 - 0.5 * g * t.^2 + v0*sin(theta0).*t;
>> x = x0 + v0*cos(theta0).*t;
>>
>> figure;
>> plot(t,x);
>> title('x(t) vs. t');
>> xlabel('Time (s)');
>> ylabel('Horizontal Distance (m)');
>> grid on;
>>
>> figure;
>> plot(t,y);
>> title('y(t) vs. t');
>> xlabel('Time (s)');
>> ylabel('Altitude (m)');
>> grid on;

Mr.Prashanth N, Asst Prof | Dept of Mechanical Engg, NMIT


[Type text] [Type text]
SIMULATION LAB 12
UNIT I SOLUTIONS

15. Solve the Following using MATLAB


a. Define the following function using syms:

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?

Mr.Prashanth N, Asst Prof | Dept of Mechanical Engg, NMIT


[Type text] [Type text]

You might also like