Question
MATLAB Find the area under the curve for the original data set. Compare it to the area under the curve for your fitted polynomial from
MATLAB
Find the area under the curve for the original data set. Compare it to the area under the curve for your fitted polynomial from under script. 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:
Script for calculating area under the curve
Must use trapz and integral
Command window output of answers
Plot of data, sorted, with line rendering style
[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, youll notice that they double back on themselves. If you just call trapz on the raw data (go ahead and do it) youll get a number thats wrong. To fix this, you need to sort the data by the x values first
The tricky part of this is you need to sort the y values by the x values if you call sort(data) youll get every column of data sorted (try it and see). If you do sort(data, 1) to sort by rows, youll 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 valuesSort the first row of data. Just like min, sort will return both the sorted numbers AND the order they ended up in
[sortedValues, sortedIndices] = sort( data(1,:) )
You can use the sortedIndices 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
Problem 1 script:
a = [2 3 4]; adtad = dlmread('Lab6Data.txt'); str = {'-r','-g-','-b'}; x = data(1,:); y = data(2,:); plot(x,y,'.') hold on for D = 1:3 p = polyfit(x,y,a(D)); y = @(I) polyval(p,I); fplot(y,[0,12],str{D}) hold on end
Lab6Data:
0.054351,0.17427,0.32544,0.38166,0.51262,0.61905,0.64539,0.84047,0.87187,0.9825,1.0579,1.2079,1.2491,1.3722,1.4461,1.581,1.707,1.7663,1.888,2.0435,2.0689,2.3991,2.7059,2.9831,3.2929,3.5675,3.9572,4.1492,4.4663,4.8072,5.1001,5.441,5.6924,5.9913,6.2658,6.6198,6.9079,7.1738,7.5331,7.7671,8.0886,8.4397,8.7064,8.9809,9.2565,9.5737,9.8301,0.10023,0.35641,0.52273,0.59058,0.89248,1.0296,1.2644,1.4489,1.743,1.7998,2.0865,2.6327,3.2776,4.0285,4.4078,5.1601,5.6326,6.2969,6.8884,7.5776,8.1572,8.7709,9.3004,9.7822 11.063,7.3294,5.1865,4.7502,3.8699,3.6388,3.3969,2.7292,2.4653,2.2054,1.9528,1.4672,1.3368,1.0796,0.97911,0.69672,0.47526,0.39946,0.17504,-0.06427,-0.10579,-0.55552,-0.91853,-1.2676,-1.4107,-1.8233,-2.0986,-2.2012,-2.3148,-2.7529,-2.8396,-2.8852,-3.0497,-3.5468,-3.4766,-3.6104,-3.7682,-3.8951,-4.0557,-4.1869,-3.9526,-4.2936,-4.3077,-4.3005,-4.4625,-4.72,-4.7369,8.7267,5.01,3.8411,3.5156,2.4489,2.0794,1.3557,0.95831,0.43264,0.33053,-0.12925,-0.86481,-1.5214,-2.0664,-2.4092,-2.7707,-3.0428,-3.5027,-3.7204,-3.9608,-4.2605,-4.546,-4.6492,-4.7869
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored 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