Question
I do not know how to fix my code, especially the 'sort' and 'trapz' clc clear D = dlmread('Lab6Data.txt'); x= D(1,:); y= D(2,:); plot(x,y,'.'); %make
I do not know how to fix my code, especially the 'sort' and 'trapz'
clc
clear
D = dlmread('Lab6Data.txt');
x= D(1,:);
y= D(2,:);
plot(x,y,'.'); %make the dot point
hold on
pp = polyfit(x,y,2); %get the fitting
f = polyval(pp,x); %get the y value for ever x on the graph p
axis([0 10 -5 15]); %limit x y range
fplot(@(x) polyval(pp,x),'-'); %make the graph
for n=3:4
p = polyfit(x,y,n); %get the fitting
f = polyval(p,x); %get the y value for ever x on the graph p
axis([0 11 -10 15]); %limit x y range
fplot(@(x) polyval(p,x),'-'); %make the graph
title('Fit');
xlabel('t');
ylabel('y');
legend('Data','Deg=2','Deg=3','Deg=4')
end
fprintf('Coefficients for polynomial: %0.4 ')
disp(pp)
Find the area under the curve for the original data set. Compare it to the area under the curve for your fitted polynomial from problem 1. Plot the sorted data to show it is sorted. Extra credit: Compare to the fitted polynomial at increasing degree (2,3,4). Are you converging (getting closer) to the answer from the original data set as you increase the degree?+ Deliverables:- 6. Script for calculating area under the curve a. Must use trapz and integral*- 7. Command window output of answers+ 8. Plot of data, sorted, with line rendering style*- 9. [Extra credit:] Answer to question, degree 3 & 4 polynomials Step by Step Instructions:*' Start with your answer to problem 1. + Calculate the area under the polynomial by using integral and the anonymous function you made for the fplot. The bounds for integral (xmin, xmax) should be the same as for fplot*- . . If you plot the data points with a line, you'll notice that they "double back" on themselves. If you just call trapz on the raw data (go ahead and do it) you'll get a number that's wrong. To fix this, you need to sort the data by the x values first o The tricky part of this is you need to sort the y values by the x values if you call sort(data) you'll get every column of data sorted (try it and see). If you do sort(data, 1) to sort by rows, you'll sort x and y independently. What you really want is what happens in Excel when you select a bunch of data and sort ALL of it by a specific column Sort by x values o Sort the first row of data. Just like min, sort will return both the sorted numbers AND the order they ended up in " [sortedValues, sortedlndices]- sort( data(1,:) ) o You can use the sortedlndices variable to sort the y values* . sortedYs data(2. sortedIndices) Once the data is sorted, you can call trapz, Plot the data with a line to verify that you have it sorted correctly Extra credit: Same as before -just add a for loop Self-check: Note: Degree 3 and 4 are extra credit+ Area original data -1X.X33905 Area poly deq 2 fitted -1X. X23052 Week 6 ENGR 112 Area poly deq 3 fitted -1X.X57785+ Area poly deq 4 fitted -1X. X00998 12 10 8 6 4 0 -2 -6 4 10Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access with AI-Powered Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started