Question

Writing Matlab code for the Least Squares Apporximation

Can I please get help with writng the matlab code for the least squares approximation and answering any theoretical questions that may come with it

7. [Least Squares Approximation] Approximate using least squares approximation with polynomials of order 5, 10, 20 on [-5,5]. Does the approximation get better higher order polynomials? Does this seem better than the result from Problem 4, 5, and 6?

0 0
Add a comment Improve this question Transcribed image text
Answer #1
function least_squares(x,n)
%least_squares(x,n) fits a least-squares polynomial of order n through
%a set of data where x and n are the coordinates.
%The output to least_squares(x,n) is the set of coefficients c0, c1,...,cm
%for the least-square polynomial Pm(x) = c0 + c1*x + c2*x^2 + ...+ cm*x^m
%Hence, there will be m+1 values in the vector c.
%A graph displays the set of points and the fitted polynomial
n = size(x,1);
if n == 1
   n = size(x,2);
end

b = zeros(m+1,1);
for i = 1:n
   for j = 1:m+1
      %right-hand side column vector consisting of sums of
      %powers of x multiplied by y's
      b(j) = b(j) + y(i)*x(i)^(j-1);
   end
end


p = zeros(2*m+1,1);
for i = 1:n
   for j = 1:2*m+1
      %sums of powers of x
      p(j) = p(j) + x(i)^(j-1);
   end
end


for i = 1:m+1
   for j = 1:m+1
      %distributing the sums of powers of x in a matrix A
      A(i,j) = p(i+j-1);
   end
end

c = A\b

%creating an x-axis and evaluating the least-ssquare polynomial
%this is only needed for data visualization
t = min(x):0.05:max(x);
n = size(t,2);
for i = 1:n
   f(i) = c(m+1);
   for j = m:-1:1
      f(i) = c(j) + f(i)*t(i);
   end
end

%data visualization
figure
plot(t,f)       %the least_squares polynomial
hold on
plot(x, n, 'r*')        %the data points
grid on
Add a comment
Know the answer?
Add Answer to:
Writing Matlab code for the Least Squares Apporximation Can I please get help with writng the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Need help with writing this code/solution in, at least, MatLab v. R2018b Task 4.12 Using the...

    Need help with writing this code/solution in, at least, MatLab v. R2018b Task 4.12 Using the command roots calculate the solutions of the equations 23 + 2a +r+1= 0 and – 2x + 25 + m2 = 4. Here you need to be careful that the coefficients of the polynomials are entered correctly as vectors. Solution 4.12 The roots can be calculated using the inline code roots([1 1 1 1]) and roots ([1 0 0 1 -2 -4]).

  • In the class, we discussed the generalized least squares approximation for higher-order (see exam...

    In the class, we discussed the generalized least squares approximation for higher-order (see example below). In this homework problem, you will need to (1) Repeat the example by preparing a Matlab code. That is, the code will first read the x and y vectors and then call (your) function to calculate a of the polynomial. (2) Calculate the standard deviation. Least Squares Approximation-higher-degree Example: Find the LS polynomial of degree three (3) that fits the following data in the table...

  • Need help with matlab code. If writing code out please make it where i can understand...

    Need help with matlab code. If writing code out please make it where i can understand and read it. I would prefer to see it in matlab form. Thanks Consider the RC Circuit shown below. Assume the excitation is a 10 kHz sine wave with amplitude of 1 volt. Derive expressions for the voltages across the resistor and the capacitor as time functions. Plot the voltages across the resistor and the capacitor over two periods using MATLAB and a reasonable...

  • Could i get help writing the code for this question in matlab? An internal combustion engine...

    Could i get help writing the code for this question in matlab? An internal combustion engine slider-crank mechanism is shown in the figure. Crank AB rotates in selected clockwise positive direction as shown. Piston position is Y=AD. o(t) is angular position of the crank, 0(t) is angular velocity of the crank, ő is angular acceleration of the crank. Crank AB rotates with a constant angular velocity of 5 rad/s clockwise in positive O direction as shown. Perform computer simulations using...

  • Can I get some help on writing a paper about please include the articles where the...

    Can I get some help on writing a paper about please include the articles where the information come from The Birth Order of the Duration and Initiation and the effects of breastfeeding

  • Circuit Analysis: Need help writing this into MATLAB. I do not need it in paper I need help putting it into MATLAB. * DO NOT WRITE ON PAPER* Please use MATLAB and post your code. Thank you. My work c...

    Circuit Analysis: Need help writing this into MATLAB. I do not need it in paper I need help putting it into MATLAB. * DO NOT WRITE ON PAPER* Please use MATLAB and post your code. Thank you. My work completed. Please help me in make code for the above circuit ^. Here is my work for Part 3 that is needed to complete this part 4. ( could not solve the last part) Part 4. AC Solution by Phasor Analysis...

  • MATLAB ONLY gauss.jpg BELOW Instructions: The following problems can be done interactively or by writing the...

    MATLAB ONLY gauss.jpg BELOW Instructions: The following problems can be done interactively or by writing the commands iın an M-file (or by a combination of the two). In either case, record all MATLAB input commands and output in a text document and edit it according to the instructions of LAB 1 and LAB 2. For problem 2, include a picture of the rank-1 approximation. For problem 3, include a picture of the rank-10 approximation and for problem 4, include a...

  • MATLAB CODE Conc. (mo/I). Time (days) 0.4 0.8 1.2 1.6 2 2.3 750 1000 1400 2000...

    MATLAB CODE Conc. (mo/I). Time (days) 0.4 0.8 1.2 1.6 2 2.3 750 1000 1400 2000 2700 3750 Now use Matlab to generate the polynomial fits for 2 through 5 order polynomials. Plot the fitted lines with an interval of 0.1 over the range of 0 to 2.7. Use "polyfit" and 'polyval” commands. Make your graph look like the one provided below: 3931 DO How good are polynomials at predicting values outside the range of observed data? What implications does...

  • How can I code this problem in MATLAB: a) Find the approximations to within 10-4 to all real zeros of the following poly...

    How can I code this problem in MATLAB: a) Find the approximations to within 10-4 to all real zeros of the following polynomials using Newton's method.? f(x)=x3 - 2*x2- 5. b) Find approximations to within 10-5 to all the zeros of each of the following polynomials by first finding the real zeros using Newton’s method and then reducing to polynomials of lower degree to determine any complex zeros. f(x)=x4 + 5x3 - 9*x2 - 85*x - 136.

  • Can someone help me figure out how to write code on Matlab for either of these...

    Can someone help me figure out how to write code on Matlab for either of these two questions? My biggest problem is working with the large factorials which Matlab can't seem to handle 1. Write a code to calculate nas a function of ga, when NA l300, 600, 3000, 6000, NB (200, 400, 2000, 4000), and g 100, plot them and try to find some tendency when N increases (hint: 4 plots). 2. Write a code to calculate the probability...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT