Beruflich Dokumente
Kultur Dokumente
and Minerals
College of Engineering Sciences
Term 191
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.
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
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:
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
2 6 10 5
4 6 10
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=
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
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