-2

The following is Exercise 3 of a Numerical Analysis task I have to do as part of my university course on the subject.

Find an approximation of tomorrow's temperature based on the last 23 values of hourly temperature of your city ( Meteorological history for Thessaloniki {The city of my univ} can be found here: http://freemeteo.com)

You will approximate the temperature function with a polynomial of 2nd, 3rd and 4th degree, using the Least Squares method. Following that, you will find the value of the function at the point that interests you. Compare your approximations qualitatively and make a note to the time and date you're doing the approximation on.

Maybe it's due to fatigue due to doing the first two tasks without break, or it's my lack of experience on numerical analysis, but I am completely stumped. I do not even know where to start.

I know it's disgusting to ask for a solution without even showing signs of effort, but I would appreciate anything. Leads, tutorials, outlines of the things I need to work on, one after the other, anything.

I'd be very much obliged to you.

NOTE: I am not able to use any MATLAB in-built approximation functions.

Dimitris Sfounis
  • 2,400
  • 4
  • 31
  • 46
  • 2
    The answer by @rody seems to make sense (so if it does not work please provide sample input). Assuming that it works, then what improvement are you looking for? -- Maybe these remarks can help you:This is quite unclear: `I am not able to use any MATLAB in-built approximation functions.`. Do you lack access to certain toolboxes, or do you have to show that you understand the inner workings of the methods. In the first case, please specify, in the latter case (and if the answer by @rody is not sufficient) please show how you would do it 'manually' and we can help with the implementation. – Dennis Jaheruddin Jan 23 '14 at 16:53
  • @DennisJaheruddin It is the latter, you have to show that you understand the analytical mathematics behind the methood. I will post the relevant scripts I have built promptly, if I find some time tonight. – Dimitris Sfounis Jan 23 '14 at 18:16

1 Answers1

4

In general, if y is your data vector belonging to the times t, and c is the coefficient vector you're interested in, then you need to solve the linear system

Ac = y

in a least-squares sense, where

A = bsxfun(@power, t(:), 0:n)

In MATLAB you can do this with mldivide:

c = A\y(:)

Example:

>> t = 0 : 0.1 : 20;              %// Define some times 
>> y = pi + 0.8*t - 3.2*t.^2;     %// Create some synthetic data
>> y = y + randn(size(y));        %// Add some noise for good measure
>> 
>> n = 2;                         %// The order of the polynomial for the fit
>> A = bsxfun(@power, t(:), 0:n); %// Design matrix
>> c = A\y(:)                     %// Solve for the coefficient matrix
c =
    3.142410118189416e+000
    7.978077631488009e-001        %// Which works pretty well
   -3.199865079047185e+000

But since you are not allowed to use any built-in functions, you can use this simple solution only to check your own outcomes. You'll have to write an implementation of the equations given on (for example) Wolfram's MathWorld.

Rody Oldenhuis
  • 37,726
  • 7
  • 50
  • 96