Sie sind auf Seite 1von 5

King Fahd University of Petroleum

and Minerals
College of Engineering Sciences

Mechanical Engineering Department

ME 413: System Dynamics and Control

Term 191

Lab 1&2: Introduction to MATLAB

Name:

ID#:

Sec.#:

Instructor:
Develop MATLAB codes for the following problems. Submit your m-files and the
MATLAB output.Points will be awardedaccordingto the effort you put in. So Do not
copy from others.

Add comments in your code to explain the commandsyou are using.

Problem 1. Find polynomials of orders 3 to 6 that would best fit the following data and plot
them against the data using an m-file. All plots should be in the same figure using
the subplot command. Consult the example in the manual for reference.

Time (sec) 0 2 4 6 8 10 12
Temperature (K) 0 293 333 341 350 343 383

The x and y axes of each subplot should be labeled. The title of each plot should be
"Order i polynomial" where i from 3 to 6.

[ .l t = [0 2 4 6 8 10 12]
y =[0 293 333 341 350 343 383]
yfit3 = polyfit(t,y,3)
subplot(2,2,1)
plot(t,polyval(yfit3,t),'r o')
hold on
plot(t,y)
hold off
title('Plot of t versus T fitted to degree 3')
xlabel('Time ')
ylabel('Temperature')
grid
legend('fitted data','measured data')

yfit4 = polyfit(t,y,4)
subplot(2,2,2)
plot(t,polyval(yfit4,t),'r o')
hold on
plot(t,y)
hold off
title('Plot of t versus T fitted to degree 4')
xlabel('Time ')
ylabel('Temperature')
grid
legend('fitted data','measured data')

yfit5 = polyfit(t,y,5)
subplot(2,2,3)
plot(t,polyval(yfit5,t),'r o')
hold on
plot(t,y)
hold off
title('Plot of t versus T fitted to degree 5')
xlabel('Time ')
ylabel('Temperature')
grid
legend('fitted data','measured data')

2
yfit6 = polyfit(t,y,6)
subplot(2,2,4)
plot(t,polyval(yfit6,t),'r o')
hold on
plot(t,y)
hold off
title('Plot of t versus T fitted to degree 6')
xlabel('Time ')
ylabel('Temperature')
grid
legend('fitted data','measured data')

]
Temperature

Temperature
Temperature

Temperature

Define the following matrix in an m-file.

4 2 6.5 1
1.5 5.5 3 7.5
8 10 2 0
4.5 1.25 2.75 3.5
Write commands to:

a) Select the second column of the matrix and store it in a variable.

A = [-4 2 -6.5 1;1.5 5.5 -3 7.5;8 10 -2 0;4.5 1.25 2.75 -3.5]


c2 = A (:,2)

c2 =

2.0000
5.5000
10.0000
1.2500

3
b) Select the following elements from the matrix and store it in a variable.

10 2 0
1.25 2.75 3.5

MATLAB code

MATLAB output

8 10 0 2
c) Replace by in the original matrix.
4.5 1.25 0.5 2

Matlab code

Matlab output

Problem 2. Define the following two polynomials in an m-file.

2 6 10 5

4 6 10

Write commands to:

a) Multiply the two polynomials and store the result in a new variable.

a = [1 2 -6 10 5]
b = [1 4 -6 10]
c = conv(a,b)

c=

1 6 -4 -16 101 -100 70 50

b) Divide polynomial p1 with polynomial p 2 . Store the quotient and the remainder in
new variables.

a = [1 2 -6 10 5]
b = [1 4 -6 10]
[q,r] = deconv(a,b)

q=

1 -2

r=

0 0 8 -12 25

4
c) Define a transfer function, . Plot the step response of the

system represented by the transfer function. [BONUS]

MATLAB code

MATLAB outputs

Problem 3. (1)-Search MATLAB help for a function for the user inputandfor the correct
syntax of if-else-if statement. Also search for the correct syntax of the disp()
function.
(2)- In an m-file, ask the user to input a number.
(3)- Usean if-else-if statement to check whether the number is greater than 75, less
than 75, equal to 75, or not a real number.
(4)- Inside the if-else-if statement, use the disp()function to show an output which
states that “the number you entered is greater…….”.

MATLAB code

MATLAB output

Das könnte Ihnen auch gefallen