Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

LAB ACTIVITY 8 . 5 . 1 : MATLAB: Least Squares Approximation This tool is provided by a third party. Though your activity may be

LAB ACTIVITY 8.5.1: MATLAB: Least Squares Approximation This tool is provided by a third party. Though your activity may be recorded, a page refresh may be needed to fill the banner. 0/1 MATLAB: Least Squares Approximation In this activity you will use a least squares approximation to find a curve of best fit for a data set. Consider the four points in the plane: (-2,3),(-1,1),(1,0), and (2,1). Use the least squares approximation to find the best-fit line for this data. Note Y is the vector b in the inconsistent system Ax=b.%Enter the data as two column vectors. X =(-2-112].' Y =[3101].'%Use the length() command to determine the size of the column vector. m = length(X)%Set up the appropriate matrix A to find the best-fit line of the form y=C+Dx. The first column Sof A will contain all l's. This is achieved here using the ones () command to create a column %vector of length m of all i's. The second column of A contains X. A =[ones (m,1) X]%Calculate the matrix products. A_transposeA = A.'* A A_transposeY = A.'* Y%Use the backslash operation to solve the overdetermined system. Soln1= A_transposeA\A_transposeY The points will range from -4 to 4 in increments of 0.1.%Generate points to plot the best-fit line. X=-4: 0.1 : 4 ylinear = Soln1(1)+ Soln1(2)*X %The following sequence of commands plots the data and the line of best fit. plot(x, ylinear, X, Y,'k*');grid;shg The MATLAB code produces the following image. 3.2.21.510.50-0.543-2.01234 A line may not be the best fit for this data. For this activity, use the same data and find the best-fit parabola.Script e Save C Reset MATLAB Documentation These are provided for you. 1%The same data is used for the activity. 2 X =[-2-112].'3 Y =[3101].'45%Use the length() command to determine the size of the column vector X. Store this value in m.6 m = length(X)7%Set up the appropriate matrix A to find the best-fit parabola of the form y=C+Dx+Ex^2. The 8%first column of A will contain all l's, using the ones) command. The second column of A 9%contains x values that are stored in X. The third column of A contains the squared x values 10%that are stored in X. Elementwise multiplication of x by itself, using .* operator, will 11%produce the desired values for the third column. 12 A =[ones (m,1) X]13%Calculate the matrix products. These are provided for you. 14 A_transposeA = A.'* A 15 A_transpose Y = A.'* Y 16|17 Use the backslash operation to solve the overdetermined system. Store this in Soln2.18 Soln2= A_transposeA\A_transposeY 19 Define the x values to use for plotting the best-fit parabola. This creates a vector x.20%This is provided for you. 21 x=-4: 0.1 : 422%Define the best-fit parabola, storing it in yquadratic. Elementwise multiplication of the 23%x values times themselves to square them is achieved by using .* operator (because x is a vector).24 yquadratic = Soln2(1)+ Soln2(2)*x 25%The following sequence of commands plots the data and the best-fit parabola. The command is 26%provided for you. 27 plot(x, yquadratic, X, Y,'k*');grid;shg 28293031

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Hands-On Database

Authors: Steve Conger

2nd Edition

0133024415, 978-0133024418

More Books

Students also viewed these Databases questions