MATLAB programming for engineers


Published on


Part of a set of FLASH lectures on using MATLAB to solve engineering problems. For full set with audio included go to:

Published in: Education
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

MATLAB programming for engineers

  1. 1. Overheads and Flash lectures for Introduction to MATLAB for engineers<br />Anthony Rossiter<br />Department of Automatic Control and Systems Engineering<br />University of Sheffield<br /><br />© University of Sheffield 2009 This work is licensed under a Creative Commons Attribution 2.0 License.<br />
  2. 2. Loops<br />Anthony Rossiter<br />ACS108<br />Department of Automatic Control and Systems Engineering<br />
  3. 3. Loops are essential<br />Code allows repeated numerical computations.<br />We do not want to write line of code 10 times to do the same thing with different numbers.<br />We want to tell the computer to use the same line, but with different numbers.<br />Look at the example on next page.<br />
  4. 4. week2_loops_essential.m<br />Open the file.<br />Two ways to compute 6 values<br />First look at lines 11-16<br />Or try lines 19-21<br />Now imagine you needed to do this for 100 different values, or a 1000.<br />Using a loop is simple to code and efficient.<br />
  5. 5. How does a loop work?<br />for k=1:6;<br /> y2(k)=sin(xvalues(k));<br />end<br />… more code<br />First k=1 hence do <br />y2(1)=sin(xvalues(1));<br />Back to start of loop<br />Now k=2 hence do <br />y2(2)=sin(xvalues(2));<br />Now k=3 hence do <br />Y2(3)=sin(xvalues(3));<br />Now k=4 hence do <br />y2(4)=sin(xvalues(4));<br />Now k=5 hence do <br />y2(5)=sin(xvalues(5));<br />Now k=6 hence do <br />y2(6)=sin(xvalues(6));<br />Now k=6 is equal to max of allowed k, continue to next line of code<br />
  6. 6. How to write the statement<br />k takes the values 3,4,5,6,7,8<br />for k=3:8<br /> code<br />end<br />for k=8:-2:2<br /> code<br />end<br />k takes the values 8,6,4,2<br />for k=4:3:13<br /> code<br />end<br />k takes the values 4,7,10,13<br />
  7. 7. General examples of for loops<br />Open the file week2_for.m<br />Note the call statement has 3 variables.<br />Run in the command window<br />Note values of k are:<br />start, start+step, start+2*step,…,start+6*step<br />
  8. 8. Nested loops<br />Open week2_for2.m<br />Note that one loop is inside another loop.<br />First k=1 and then l goes from 1 to 5.<br />First k=2 and then l goes from 1 to 5.<br />….<br />Finally k=10 and then l goes from 1 to 5.<br />Inner loop<br />Outer loop<br />Run the file and experiment with changing the loop for statements<br />
  9. 9. TASKS<br />Use help week2_loop_for_plotting.m and determine how to run this file and also how it works.<br />Write a function file which takes as its input a vector of arbitrary length and then use a loop to define the output vector whose values are the cubes of the coefficients of the first vector plus one. The syntax could be:<br />output_vector= week2_loop_cube(input_vector) <br />Write a file to compute savings growth over time. For instance, let savings with an interest rate of 5% and annual bonus of £5 grow according to the model: Write a file with syntax<br />Pounds=savings_growth(Pounds_in, interest_rate, annual_bonus,years_invested)<br />The file should also plot savings growth.<br />
  10. 10. This resource was created by the University of Sheffield and released as an open educational resource through the Open Engineering Resources project of the HE Academy Engineering Subject Centre. The Open Engineering Resources project was funded by HEFCE and part of the JISC/HE Academy UKOER programme.<br />© 2009 University of Sheffield<br />This work is licensed under a Creative Commons Attribution 2.0 License.<br />The JISC logo is licensed under the terms of the Creative Commons Attribution-Non-Commercial-No Derivative Works 2.0 UK: England & Wales Licence.  All reproductions must comply with the terms of that licence.<br />The HEA logo is owned by the Higher Education Academy Limited may be freely distributed and copied for educational purposes only, provided that appropriate acknowledgement is given to the Higher Education Academy as the copyright holder and original publisher.<br />The name and logo of University of Sheffield is a trade mark and all rights in it are reserved. The name and logo should not be reproduced without the express authorisation of the University.<br />Where Matlab® screenshots are included, they appear courtesy of The MathWorks, Inc. <br />