0% found this document useful (0 votes)
13 views5 pages

WS1

This document is a worksheet for an Advanced MATLAB course, focusing on algorithms such as integration, differentiation, and Newton's method. It includes a series of exercises that guide students through creating scripts to find maximum and minimum values in arrays, implement Newton's method for root finding, perform numerical integration, and practice numerical differentiation. The document emphasizes the importance of understanding built-in functions and encourages students to revise their first-year MATLAB skills.

Uploaded by

Xhoni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views5 pages

WS1

This document is a worksheet for an Advanced MATLAB course, focusing on algorithms such as integration, differentiation, and Newton's method. It includes a series of exercises that guide students through creating scripts to find maximum and minimum values in arrays, implement Newton's method for root finding, perform numerical integration, and practice numerical differentiation. The document emphasizes the importance of understanding built-in functions and encourages students to revise their first-year MATLAB skills.

Uploaded by

Xhoni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Faculty of Engineering

Advanced MATLAB

Worksheet 1 – Recap of algorithms

This worksheet is intended to be a recap of some of the things we learnt during the first MATLAB
course, it covers basic algorithms, including but not limited to integration, differentiation and
Newton’s method. It assumes you have a good grasp of the MATLAB skills you learnt in the 1st
year, including but not limited to if statements, arrays, for loops, while loops etc... If you struggle
with the questions here, then go back and spend a weekend revising the MATLAB we learnt during
the 1st year.

Finding the maximum and minimum

Q1: Although MATLAB provides a lot of built in functions such as max and min. It is often a good
idea to at least have an idea how the built in functions work. This example demonstrates how the
min and max functions work. Make a new script file called q1.m

a) Define an array ten of numbers by hand between 0 and 100, call this array 'b'

b) Define a new variable my_max by setting it equal to the 1st element of array 'b'

c) Write a for loop that loops from 1 to the length of array 'b'

d) Write an if statement within the for loop that checks if the ith value of array b is bigger than
my_max. If this condition is met set my_max to the ith value of the array b.

e) Add an sprintf command at the bottom of the script to print 'The maximum of the array is ??',
where ?? is value of my_max.

f) The built in functions in MATLAB such as max and min, are good but often don't do exactly
what you want them to do. Now we are going to produce a modified function which only works on
the first half of the data in the array. Do this by either inserting an if statement into the for loop or
by changing the for range of the loop.

Q2: In this example you will change the script q1.m to find the minimum value of an array instead
of the maximum value. First, copy the script file q1.m to q2.m. Change the variable name my_max
to my_min, now change the if statement to check if the ith element of array 'b' is smaller than
my_min. Update the sprintf statement accordingly. Your script should now find the minimum
value in the array.

Netwon’s method

Q3: Newton’s method is often used to find the root of an equation. In this section we will be
practising Newton’s method. In the notes Newton’s an example of Newton’s method is given as:

x=10 %our guess for the answer


for i=1:10 %loop ten times
y=x*x+10*x-3; %f(x)
dy=2*x+10; %f'(x)
new_x=x-(y/dy); %calculate new x
x=new_x; %update x
disp(x) %print the answer
end
Faculty of Engineering
Advanced MATLAB

a) Adapt this code to find the roots of the equation (x-3)(x-50)(x-10), instead of x*x+10*x-3.

b) Adapt the code using an input statement to ask the user what the initial guess of the answers
should be. How does the initial guess affect which root of the equation is found, play around with
your code.

c) Adjust the loop so that it runs for 40 times instead of 10. Then at the beginning of the code
initialize an array called ‘convergence’ of length 40 and add a line of code within the loop to store
the current value of x in the array ‘convergence’, at position ‘i’. So if ‘i’ is 4 and the loop has run
four times, the value of x is stored in position 4 in the array.

d) Add a line to the bottom of the script to plot the array convergence.

e) What happens if you get the derivative a bit wrong? Try multiplying the calculated value of the
by 0.9, and rerunning the code. How does this affect the ability of the algorithm to find the answer?

f) Adjust the script so that when the value of x changes by less than 1e-6 the loop exits early. (Hint
use if abs(x-x_new), and the break command).

Q4:
a) Make an array ‘x’ with 200 points from 0 to 10*pi.

b) Take sin(x)cos(x) of each of the points in the array and store the results in a new array called y.

c) Plot a graph of x against y.

d) By hand make a note of what the first 4 roots of y=sin(x) will be.

e) Use Newton’s method to calculate the first 4 roots of sin(x), use your graphical plot to inform
which guesses you should give Newton’s method to find the roots.

f) Place the whole of Newton’s method in a loop which will change the value of the initial guess
from 0 to 10*pi which Newton’s method uses. This method called searching for a root, and is very
powerful because it will give you the roots for an unknown equation. How many roots does
Newton’s method find between 0 and 10*pi.

g) [hard skip if you want to] For each new root your code finds add it to an array called my_roots.
If the value already appears in my_roots then don’t add it to the array.

Integrating

Q5: Integrating is just finding the area under a curve. In this question we are going to do a
numerical integration of the equation y=x 2 , between 0 and 2 by hand:

a) On the piece paper sketch the curve y=x 2 . Do this by first calculating the value of y at x=0,
x=1 and x=2. Using a pen or a pencil, draw a smooth curve joining the dots. Now using a pencil,
draw two rectangular boxes which approximate the area under the curve between x=0 and x=2.
What is the total area of the boxes using this method?
Faculty of Engineering
Advanced MATLAB

b) Now using what you learnt in mathematics integrate the equation y=x 2 between the limits
x=0 and x=2. What's the area under the curve using this method?

c) Why do the answers for a and b not match? What could you do to make the answers match?

Q6: Numerical integration on a computer works just like the example in question 5a. In this
question we will be integrating the equation y=x 2 between x=0 and x=2 using MATLAB.

a) Make a new script file called q6.m in this script file make a while loop that will continue running
as long as x is smaller or equal to 2. You should initialize x to 0 at the start of the script. The loop
should count up in steps of 0.1, the step size should be initialized as a variable dx at the start of the
program.

b) Inside the while loop, calculate the value of 'y' for each value of 'x'. Multiply y by the step size to
calculate the area of each integration box store this value in the variable 'box_area'.

c) Initialize a variable at the top of the script called 'sum', this will hold the sum of all boxes. Add
the area of each box to 'sum' each time the loop runs.

d) Using the analytical expression for the integral between 0 and 2 you calculated in question 5b,
define a variable error which will hold the difference between the numerical and analytical
expressions. Print out the error at the end of the script using sprintf.

e) Congratulations you have just numerically integrated your first function! Try changing the step
size dx from 0.5 to 0.0001. What happens to the error? Why is this?

Q7: Copy the script you write in q6.m to a new file called q7.m. Remove the lines which calculate
the error.

a) The script only integrates between 0 and 2. Change the script so that it asks the user for the two
limits of integration. If the second limit is smaller than the first limit the program should swap the
first and second limit so the program continues to run. If the limits are both the same then the
program should print the warning message 'Limits of integration are the same'

Now integrate the following functions by using your script:

b) sin( x ) between 1 and 2

c) cos ( x) between 1 and 2

d) x 3 between 1 and 2

e) cos (cos( x)) between 2 and 4

f) e−x between 0 and 1

g) sin(x ) e−x between 0 and 1

Congratulations you can now integrate any function you want to between any limits!
Faculty of Engineering
Advanced MATLAB

Differentiating

Q8: In this question you will practice numerical differentiation of functions. Differentiation, is just
finding the gradient of a line. The simplest way to differentiate a function is to calculate it's y value
at one place on a curve, calculate it's y value at another place on the curve. Then calculate the
difference between the y values and the x values at the two points. This was explained during the
lecture.

a) Make a script to ask the user for two points on the x-axis, x1 and x2.
2
b) For the equation y=x calculate the value of y at each point, and store the values in y1 and y2

c) Calculate the difference between x2 and x1 and store this in a new variable dx.

d) Calculate the difference between y2 and y1 and store this in a new variable dy.

e) Divide dy by dx to calculate the gradient of y=x 2 between the two points, store the value in
numerical_dydx.
2
f) Differentiate y=x by hand and evaluate it mid way between x2 and x1, store the result in
analytical_dydx.

g) Subtract analytical_dydx and numerical_dydx. Why is there a difference between the values.
Try choosing points of x1 and x2 closer together. What happens to the error?

Q9: Copy the script you made in q8, to q9.m. Remove the line which calculate the error and replace
the function y=x 2 with the following functions and find the differential at x=1:

b) sin(x )

c) cos ( x)
3
d) x

e) cos (cos( x))

f) e−x

g) sin(x ) e−x

Q10: Write a script to plot sin(x) between 0 and 4pi using 100 points. Now edit the script to
calculate the numerical derivative of sin(x) at every point on the x-axis.

Differentiating and Newton’s method (quite hard)

Q11: Combine Newton’s method and numerical differentiation so to solve the equation sin(x)tan(x),
but not using an analytical derivative. Use instead a numerical derivative. Once way to do this is to
Faculty of Engineering
Advanced MATLAB

take the derivative over a small range of x, such as 1x10-6.

You might also like