You are on page 1of 20

Loops

Wednesday, 4 April 2018 9:01 PM

Consider the following vector:

We want to square each element in the X vector.


We can do this with element-by-element operators:

But what if we wanted to square each element by going through each element of the X vector
individually and squaring that particular element. To go through each element of a vector, we
can use for loops!

The syntax for the for loop is:

To go through each element of a vector using a for loop, we need to supply the counter with
the indices (element numbers) of the vector we are interested in. E.g.

Writing it out explicitly, the i counter vector is:

Even though the i counter is supplied as a vector above, i will only take on one value at a time in
the for loop. For the first iteration, i = 1. For the second iteration, i=2. For the third iteration, i=
3, and so forth.

So to square the elements of X using a for loop, we can address the elements of X individually
using i and square them as scalars.

To illustrate the for loop in action, I've written out the first 3 iterations for the body commands:

ENG1060 Page 1
To illustrate the for loop in action, I've written out the first 3 iterations for the body commands:

Note that Xsqrd =25 before this iteration. Now we're saying that the second element of Xsqrd is
100. MATLAB will automatically expand your Xsqrd vector to accommodate the second
element. So after the second iteration, Xsqrd = [25, 100].

Now, Xsqrd is currently [25, 100, 225]. The for loop will continue to iterate until the length(X)
(which is equal to 5).

Let's say now we want to store values of X^2 that are even into a different variable named
Xeven. Again, we can use a for loop to address all elements of the X vector individually.
However, now we need to use a condition to check whether the X^2 element is even or not.
This is achieved with an if condition.

ENG1060 Page 2
You can see this in action by writing it out (see below) or using the debugger tool to step it
through line-by-line.

Even though we did not have a result stored into the


1st element of Xeven, MATLAB will automatically
expand the vector and consider those unfilled
elements as 0. Therefore, after the 2nd iteration
Xeven = [0, 100].

ENG1060 Page 3
After all 5 iterations, Xeven = [0, 100, 0, 400]. Recall that we only wanted to store the X^2
values that were even, but we have 0 values in the Xeven vector. This is because the i counter is
not appropriate for what we want to achieve here. It is important to note that the counter
variable isn't always appropriate for indexing. You may need to create your own counter
variable. E.g.

A quick overview of the new code:

ENG1060 Page 4
Note how this result is different to the previous example which used i
as an index for Xeven. In the previous example, Xeven = [0, 100] after
the 2nd iteration. Here, the 0 was not stored and so Xeven = 100.

After all 5 iterations, Xeven = [100, 400]. It is important to note that you will need to create
your own counters (index variables) for while loops since while loops do not natively have a
counter variable like for loops. That is, create your counter variable outside the loop and then
update it inside the loop as necessary.

ENG1060 Page 5
Map_root_finding
Tuesday, 11 September 2018 2:23 PM

ENG1060 Page 1
ENG1060 Page 2
Map_curve_fitting
Wednesday, 12 September 2018 2:11 AM

ENG1060 Page 1
ENG1060 Page 2
polyfit and polyval
Wednesday, 25 April 2018 10:21 AM

Consider the following data set:


x = 1:5;
y = [11, 16, 30, 63, 157];

We can fit numerous models to this data. Some are more appropriate than others. The models considered
in ENG1060 are:
• Linear model (1st order polynomial)
• Polynomials
• Exponential model
• Power model
• Saturation growth model

The polyfit(x,y,n) function fits a POLYNOMIAL to x and y of order n.

A linear model takes the form of :

So we can fit a straight line to our x and y data with:

Another example. A third-order polynomial takes the form:

We can fit a third-order polynomial to the x and y data with:

ENG1060 Page 1
So polyfit gives us the coefficients of a POLYNOMIAL. How can we use this?
Since we know the form of the model and its coefficients, we can calculate the
dependent values (y values) for any independent values (x values).

For example, let's say we've determined our third-order polynomial coefficients using p = polyfit(x,y,3)
and we want to find out what the value is at x=3.5 (we don't have this exact point in our original data).
We can manually construct our polynomial and then substitute in x=3.5 as follows.

This is tedious work! You should NOT need to construct your own polynomials because MATLAB has a
function to do this for you! The function is called polyval.

The polyval(p,xfit) function takes coefficients of a polynomial (i.e. p) and


substitutes xfit into the polynomial to return the corresponding dependent
variable.
So the above yfit can be achieved using polyfit as follows:

Let's find the value of y for x=4.2 based on a 5th-order polynomial fit. First find the coefficients using
polyfit. Then use polyval to evaluate for x=4.2 with the supplied coefficients.

Often we want to see how our polynomial fits over some range of the data set. That is, we want to use
polyval on a vector of values. Since our x data ranges from 1 to 5, I will create a new x variable for my
fitted function called xfit which spans a little out of this range.

Plotting yfit against xfit on the same plot at the top of this document gives:

ENG1060 Page 2
Plotting yfit against xfit on the same plot at the top of this document gives:

So now you understand polyfit and polyval. It is crucial to remember that polyfit
fits POLYNOMIALS and polyval takes POLYNOMIAL coefficients to evaluate
values.

But wait (there's more!), the exponential, power and saturation-growth models
aren't polynomials. How do we use polyfit and polyval with those models?
Firstly, you can't use polyfit to fit any of those models and you can't use polyval to evaluate values with
any of those models. What you can do is linearise the data so that you can perform LINEAR REGRESSION
to obtain a1 and a0, and then transform those linear variables back to the nonlinear coefficients. For
example:

The exponential model takes the form:

As we've learned, we can determine alpha and beta by


• Linearising the model to the form of y=a1*x + a0
• Perform linear regression on the linearised variables to determine a1 and a0
• Apply appropriate transformations on a1 and a0 to determine alpha and beta

Linearising the exponential model gives us:

So our transformation relationships are:

We want to fit a line of best fit to our LINEARISED variables (x_linear and y_linear). Remember that
polyfit provides the coefficients of a polynomial. Therefore, we can supply polyfit with our LINEARISED
variables and a 1st order polynomial.

ENG1060 Page 3
polyfit provides the coefficients of a polynomial. Therefore, we can supply polyfit with our LINEARISED
variables and a 1st order polynomial.

We can determine alpha and beta by referring back to our transformation relationships.

Now we know the coefficients of our nonlinear model. We can determine the dependent values (y
values) for any independent values (x values). Again, we can't use polyval here because polyval only
works with polynomial coefficients. If you supply p to polyval then you will get an evaluation of x based
on a linear model (since we used order of 1 for polyfit).

Instead, you want to create a function handle (or vector) for your nonlinear equation. For example, let's
say we wanted determine all the y values from x=0 to 6 with 200 points, based on our exponential
equation.

So we only used polyfit to fit our LINEARISED data. We don't use polyfit on the original data set if we
want to fit an exponential, power or saturation growth model to it. Also, we don't use polyval for any of
these models because they are not polynomials. We can use polyval if we wanted to plot in the
linearised space. That is, if we were to plot y_linear against x_linear. E.g.

ENG1060 Page 4
ENG1060 Page 5
Map_integration
Wednesday, 3 October 2018 7:58 AM

ENG1060 Page 1
ENG1060 Page 2
Map_ODEs
Wednesday, 3 October 2018 11:46 AM

ENG1060 Page 1
ENG1060 Page 2
ENG1060 Page 3
ENG1060 Page 4

You might also like