# MatLab Script Exercise

Announcements
#1
Please could somebody help me with this MatLab script-writing exercise? I'm very confused and don't know where to start. I'm not sure what it's asking. I have been given this background information and these instructions:
1. The least-squares method involves the minimisation of the root-mean-squared (r.m.s.) value of the differences between real data points and fitted data values :As we are only interested in minimising this formula and not in the value of , we can drop the the to obtain:For a straight-line fit of , we can write this as:If we take derivatives of with respect to and , we obtain the equations:andWrite a function that calculates the summations, solves for and for input arrays x and y and returns , and the r.m.s. value for the data set. You should then write a script that calls this function and prints both a list of ,, and values and the values of , and . Your script should also plot and against .2. Having fitted linear data using the least-squares method, it is now time to try fitting higher-order polynomials.First, you should write a function for generating data. Your data will be obtained from a polynomial with coefficients to with an added Gaussian error term :Here, is the experimental r.m.s. value we wish our data to have, and is a Gaussian random number with mean 0 and variance 1. This can be done by generating a pair of random numbers and between 0 and 1 using the rand() function. You can then obtain a Guassian random number via:The MATLAB built-in function randn() is equivalent to this and returns an array of normally-distributed numbers with mean 0 and standard deviation 1. MATLAB also has a function normrnd() in its Statistics Toolbox which takes the mean and standard deviation as arguments along with the size of the resulting output. As usual, the help normrnd and help randn commands document all the details of how to use these two functions.Note that a linear fit is the special case where the polynomial is of order 1.Next, you should try curve fitting polynomials. In this case, for a polynomial of order you end up with equations and unknowns:where is the index of the equation and:The equation:may be solved in two ways. The first (and mathematically intuitive) way is to multiply by the inverse of :or in MATLAB:c = inv(A) * v;This works, but is rather slow and has issues with numerical precision. A better way is to perform Gaussian row reduction to obtain the reduced-row-echelon form of the matrix. This process is described in detail in the vectors and matrices course.MATLAB has two options for Gaussian elimination. The first is to use the rref() function, which returns the row-reduced echelon form of an matrix:row_reduced_form = rref([A, v]).
0
X

new posts Back
to top
Latest
My Feed

### Oops, nobody has postedin the last few hours.

Why not re-start the conversation?

see more

### See more of what you like onThe Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.

### Poll

Join the discussion

Yes (130)
66.33%
No (44)
22.45%
I didn't use it to prepare (22)
11.22%