SlideShare una empresa de Scribd logo
1 de 10
Overheads and Flash lectures for Introduction to MATLAB for engineers Anthony Rossiter Department of Automatic Control and Systems Engineering University of Sheffield www.shef.ac.uk/acse © University of Sheffield 2009 This work is licensed under a Creative Commons Attribution 2.0 License.
Loops Anthony Rossiter ACS108 Department of Automatic Control and Systems Engineering
Loops are essential Code allows repeated numerical computations. We do not want to write line of code 10 times to do the same  thing with different numbers. We want to tell the computer to use the same line, but with different numbers. Look at the example on next page.
week2_loops_essential.m Open the file. Two ways to compute 6 values First look at lines 11-16 Or try lines 19-21 Now imagine you needed to do this for 100 different values, or a 1000. Using a loop is simple to code and efficient.
How does a loop work? for k=1:6;     y2(k)=sin(xvalues(k)); end … more code First k=1 hence do  y2(1)=sin(xvalues(1)); Back to start of loop Now k=2 hence do  y2(2)=sin(xvalues(2)); Now k=3 hence do  Y2(3)=sin(xvalues(3)); Now k=4 hence do  y2(4)=sin(xvalues(4)); Now k=5 hence do  y2(5)=sin(xvalues(5)); Now k=6 hence do  y2(6)=sin(xvalues(6)); Now k=6 is equal to max of allowed k, continue to next line of code
How to write the statement k takes the values 3,4,5,6,7,8 for k=3:8     code end for k=8:-2:2     code end k takes the values 8,6,4,2 for k=4:3:13     code end k takes the values 4,7,10,13
General examples of for loops Open the file week2_for.m Note the call statement has 3 variables. Run in the command window Note values of k are: start, start+step, start+2*step,…,start+6*step
Nested loops Open week2_for2.m Note that one loop is inside another loop. First k=1 and then l goes from 1 to 5. First k=2 and then l goes from 1 to 5. …. Finally k=10 and then l goes from 1 to 5. Inner loop Outer loop Run the file and experiment with changing the loop for statements
TASKS Use help week2_loop_for_plotting.m and determine how to run this file and also how it works. 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: output_vector= week2_loop_cube(input_vector)  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 Pounds=savings_growth(Pounds_in, interest_rate, annual_bonus,years_invested) The file should also plot savings growth.
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. © 2009 University of  Sheffield This work is licensed under a Creative Commons Attribution 2.0 License. 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. 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. 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. Where Matlab® screenshots are included, they appear courtesy of  The MathWorks, Inc.

Más contenido relacionado

Destacado (8)

International Economics
International EconomicsInternational Economics
International Economics
 
2.2 polynomial functions of higher degree
2.2 polynomial functions of higher degree2.2 polynomial functions of higher degree
2.2 polynomial functions of higher degree
 
Zeroes and roots
Zeroes and rootsZeroes and roots
Zeroes and roots
 
Section 4.1 polynomial functions and models
Section 4.1 polynomial functions and models Section 4.1 polynomial functions and models
Section 4.1 polynomial functions and models
 
State equations for physical systems
State equations for physical systemsState equations for physical systems
State equations for physical systems
 
Polynomial Function and Synthetic Division
Polynomial Function and Synthetic DivisionPolynomial Function and Synthetic Division
Polynomial Function and Synthetic Division
 
Polynomial functions
Polynomial functionsPolynomial functions
Polynomial functions
 
Matlab for Electrical Engineers
Matlab for Electrical EngineersMatlab for Electrical Engineers
Matlab for Electrical Engineers
 

Similar a MATLAB programming for engineers

CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docx
CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docxCMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docx
CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docx
monicafrancis71118
 
Estimating model-sample-pages
Estimating model-sample-pagesEstimating model-sample-pages
Estimating model-sample-pages
manxman123
 
15LLP108_Demo4_LedBlinking.pdf1. Introduction In D.docx
15LLP108_Demo4_LedBlinking.pdf1. Introduction In D.docx15LLP108_Demo4_LedBlinking.pdf1. Introduction In D.docx
15LLP108_Demo4_LedBlinking.pdf1. Introduction In D.docx
felicidaddinwoodie
 
Yeahhhh the final requirement!!!
Yeahhhh the final requirement!!!Yeahhhh the final requirement!!!
Yeahhhh the final requirement!!!
olracoatalub
 

Similar a MATLAB programming for engineers (20)

CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docx
CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docxCMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docx
CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docx
 
Applicative Logic Meta-Programming as the foundation for Template-based Progr...
Applicative Logic Meta-Programming as the foundation for Template-based Progr...Applicative Logic Meta-Programming as the foundation for Template-based Progr...
Applicative Logic Meta-Programming as the foundation for Template-based Progr...
 
Estimating model-sample-pages
Estimating model-sample-pagesEstimating model-sample-pages
Estimating model-sample-pages
 
Cmps 260, fall 2021 programming assignment #3 (125 points)
Cmps 260, fall 2021 programming assignment #3 (125 points)Cmps 260, fall 2021 programming assignment #3 (125 points)
Cmps 260, fall 2021 programming assignment #3 (125 points)
 
notesnet.dk - Eclipse Modelling Tools
notesnet.dk - Eclipse Modelling Toolsnotesnet.dk - Eclipse Modelling Tools
notesnet.dk - Eclipse Modelling Tools
 
15LLP108_Demo4_LedBlinking.pdf1. Introduction In D.docx
15LLP108_Demo4_LedBlinking.pdf1. Introduction In D.docx15LLP108_Demo4_LedBlinking.pdf1. Introduction In D.docx
15LLP108_Demo4_LedBlinking.pdf1. Introduction In D.docx
 
Mini Project- ROM Based Sine Wave Generator
Mini Project- ROM Based Sine Wave GeneratorMini Project- ROM Based Sine Wave Generator
Mini Project- ROM Based Sine Wave Generator
 
Mini Project- Torque Control of a DC Motor
Mini Project- Torque Control of a DC MotorMini Project- Torque Control of a DC Motor
Mini Project- Torque Control of a DC Motor
 
Ecet 365 Enhance teaching / snaptutorial.com
Ecet 365   Enhance teaching / snaptutorial.comEcet 365   Enhance teaching / snaptutorial.com
Ecet 365 Enhance teaching / snaptutorial.com
 
Ecet 365 Education Redefined - snaptutorial.com
Ecet 365    Education Redefined - snaptutorial.comEcet 365    Education Redefined - snaptutorial.com
Ecet 365 Education Redefined - snaptutorial.com
 
Cost estimation using cocomo model
Cost estimation using cocomo modelCost estimation using cocomo model
Cost estimation using cocomo model
 
Functional Smalltalk
Functional SmalltalkFunctional Smalltalk
Functional Smalltalk
 
A logic foundation for template-based program transformation in Eclipse
A logic foundation for template-based program transformation in EclipseA logic foundation for template-based program transformation in Eclipse
A logic foundation for template-based program transformation in Eclipse
 
ECET 365 Exceptional Education / snaptutorial.com
ECET 365 Exceptional Education / snaptutorial.comECET 365 Exceptional Education / snaptutorial.com
ECET 365 Exceptional Education / snaptutorial.com
 
Object Oriented Concepts and Principles
Object Oriented Concepts and PrinciplesObject Oriented Concepts and Principles
Object Oriented Concepts and Principles
 
Yeahhhh the final requirement!!!
Yeahhhh the final requirement!!!Yeahhhh the final requirement!!!
Yeahhhh the final requirement!!!
 
Switch case and looping jam
Switch case and looping jamSwitch case and looping jam
Switch case and looping jam
 
1.01.Introduction.pptx
1.01.Introduction.pptx1.01.Introduction.pptx
1.01.Introduction.pptx
 
_OOP with JAVA Solution Manual (1).pdf
_OOP with JAVA Solution Manual (1).pdf_OOP with JAVA Solution Manual (1).pdf
_OOP with JAVA Solution Manual (1).pdf
 
An Introduction to OMNeT++ 6.0
An Introduction to OMNeT++ 6.0An Introduction to OMNeT++ 6.0
An Introduction to OMNeT++ 6.0
 

Último

The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 

Último (20)

How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 

MATLAB programming for engineers

  • 1. Overheads and Flash lectures for Introduction to MATLAB for engineers Anthony Rossiter Department of Automatic Control and Systems Engineering University of Sheffield www.shef.ac.uk/acse © University of Sheffield 2009 This work is licensed under a Creative Commons Attribution 2.0 License.
  • 2. Loops Anthony Rossiter ACS108 Department of Automatic Control and Systems Engineering
  • 3. Loops are essential Code allows repeated numerical computations. We do not want to write line of code 10 times to do the same thing with different numbers. We want to tell the computer to use the same line, but with different numbers. Look at the example on next page.
  • 4. week2_loops_essential.m Open the file. Two ways to compute 6 values First look at lines 11-16 Or try lines 19-21 Now imagine you needed to do this for 100 different values, or a 1000. Using a loop is simple to code and efficient.
  • 5. How does a loop work? for k=1:6; y2(k)=sin(xvalues(k)); end … more code First k=1 hence do y2(1)=sin(xvalues(1)); Back to start of loop Now k=2 hence do y2(2)=sin(xvalues(2)); Now k=3 hence do Y2(3)=sin(xvalues(3)); Now k=4 hence do y2(4)=sin(xvalues(4)); Now k=5 hence do y2(5)=sin(xvalues(5)); Now k=6 hence do y2(6)=sin(xvalues(6)); Now k=6 is equal to max of allowed k, continue to next line of code
  • 6. How to write the statement k takes the values 3,4,5,6,7,8 for k=3:8 code end for k=8:-2:2 code end k takes the values 8,6,4,2 for k=4:3:13 code end k takes the values 4,7,10,13
  • 7. General examples of for loops Open the file week2_for.m Note the call statement has 3 variables. Run in the command window Note values of k are: start, start+step, start+2*step,…,start+6*step
  • 8. Nested loops Open week2_for2.m Note that one loop is inside another loop. First k=1 and then l goes from 1 to 5. First k=2 and then l goes from 1 to 5. …. Finally k=10 and then l goes from 1 to 5. Inner loop Outer loop Run the file and experiment with changing the loop for statements
  • 9. TASKS Use help week2_loop_for_plotting.m and determine how to run this file and also how it works. 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: output_vector= week2_loop_cube(input_vector)  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 Pounds=savings_growth(Pounds_in, interest_rate, annual_bonus,years_invested) The file should also plot savings growth.
  • 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. © 2009 University of Sheffield This work is licensed under a Creative Commons Attribution 2.0 License. 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. 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. 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. Where Matlab® screenshots are included, they appear courtesy of The MathWorks, Inc.