In: Advanced Math
**MATLAB
8)The structure for students' quiz data for a class is organized as below
ID number | Quiz |
44 | 7 |
33 | 5.5 |
37 | 8 |
write a script to print the students' data by ascending order of ID number. The index vector method must be used and the MATLAB functions for sorting CANNOT be used.
9)In a physics measurement, the density of the water is measured at different depth. Here are the depth vector and density vector.
depth=[100,200,300,400,500]
density=[6.1,6.9,8.0,8.8,10.2]
USE polyfit to fit the data with 1,2, and 3 degree curves and use subplot to over plot your fitting curves on your original data.
MATLAB Code:
close all
clear
clc
% Problem 8
id = [44 33 37];
quiz = [7 5.5 8];
for i = 1:length(id)
for j = i+1:length(id)
if id(i) > id(j)
temp = id(i);
id(i) = id(j);
id(j) = temp;
temp = quiz(i);
quiz(i) = quiz(j);
quiz(j) = temp;
end
end
end
disp('After Sorting:')
disp('ID:'), disp(id)
disp('Quiz:'), disp(quiz)
% Problem 9
depth = [100 200 300 400 500];
density = [6.1 6.9 8.0 8.8 10.2];
depth_ = min(depth)-10:0.1:max(depth)+10;
p1 = polyfit(depth, density, 1); y1 = polyval(p1, depth_);
p2 = polyfit(depth, density, 2); y2 = polyval(p2, depth_);
p3 = polyfit(depth, density, 3); y3 = polyval(p3, depth_);
figure
subplot(131), plot(depth, density, 'o', depth_, y1)
xlabel('Depth'), ylabel('Density')
title('Degree 1 Polynomial')
subplot(132), plot(depth, density, 'o', depth_, y2)
xlabel('Depth'), ylabel('Density')
title('Degree 2 Polynomial')
subplot(133), plot(depth, density, 'o', depth_, y3)
xlabel('Depth'), ylabel('Density')
title('Degree 3 Polynomial')
Output:
After Sorting:
ID:
33 37 44
Quiz:
5.5000 8.0000 7.0000
Plot: