SlideShare a Scribd company logo
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.

More Related Content

Viewers also liked

International Economics
International EconomicsInternational Economics
International Economics
Sandip Das
 
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
Ron Eick
 
Zeroes and roots
Zeroes and rootsZeroes and roots
Zeroes and roots
Nandeesh Laxetty
 
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
Wong Hsiung
 
State equations for physical systems
State equations for physical systemsState equations for physical systems
State equations for physical systems
Sarah Krystelle
 
Polynomial Function and Synthetic Division
Polynomial Function and Synthetic DivisionPolynomial Function and Synthetic Division
Polynomial Function and Synthetic Division
Elceed
 
Polynomial functions
Polynomial functionsPolynomial functions
Polynomial functions
Malikahmad105
 
Matlab for Electrical Engineers
Matlab for Electrical EngineersMatlab for Electrical Engineers
Matlab for Electrical Engineers
Manish Joshi
 

Viewers also liked (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 to 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
 
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...
Coen De Roover
 
Estimating model-sample-pages
Estimating model-sample-pagesEstimating model-sample-pages
Estimating model-sample-pages
manxman123
 
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)
mehek4
 
notesnet.dk - Eclipse Modelling Tools
notesnet.dk - Eclipse Modelling Toolsnotesnet.dk - Eclipse Modelling Tools
notesnet.dk - Eclipse Modelling Tools
Tonny Madsen
 
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
 
Mini Project- ROM Based Sine Wave Generator
Mini Project- ROM Based Sine Wave GeneratorMini 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
Ecet 365 Enhance teaching / snaptutorial.com
Ecet 365   Enhance teaching / snaptutorial.comEcet 365   Enhance teaching / snaptutorial.com
Ecet 365 Enhance teaching / snaptutorial.com
Davis116a
 
Ecet 365 Education Redefined - snaptutorial.com
Ecet 365    Education Redefined - snaptutorial.comEcet 365    Education Redefined - snaptutorial.com
Ecet 365 Education Redefined - snaptutorial.com
DavisMurphyC85
 
Cost estimation using cocomo model
Cost estimation using cocomo modelCost estimation using cocomo model
Cost estimation using cocomo model
Nitesh Bichwani
 
Functional Smalltalk
Functional SmalltalkFunctional Smalltalk
Functional Smalltalk
ESUG
 
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
Coen De Roover
 
ECET 365 Exceptional Education / snaptutorial.com
ECET 365 Exceptional Education / snaptutorial.comECET 365 Exceptional Education / snaptutorial.com
ECET 365 Exceptional Education / snaptutorial.com
donaldzs109
 
Object Oriented Concepts and Principles
Object Oriented Concepts and PrinciplesObject Oriented Concepts and Principles
Object Oriented Concepts and Principles
deonpmeyer
 
Yeahhhh the final requirement!!!
Yeahhhh the final requirement!!!Yeahhhh the final requirement!!!
Yeahhhh the final requirement!!!
olracoatalub
 
Switch case and looping jam
Switch case and looping jamSwitch case and looping jam
Switch case and looping jam
JamaicaAubreyUnite
 
1.01.Introduction.pptx
1.01.Introduction.pptx1.01.Introduction.pptx
1.01.Introduction.pptx
Binish Raza
 
_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
vanithagp1
 
An Introduction to OMNeT++ 6.0
An Introduction to OMNeT++ 6.0An Introduction to OMNeT++ 6.0
An Introduction to OMNeT++ 6.0
Alpen-Adria-Universität
 

Similar to 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
 

Recently uploaded

PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
AyyanKhan40
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
Nicholas Montgomery
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
tarandeep35
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
adhitya5119
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
David Douglas School District
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
Jean Carlos Nunes Paixão
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
RitikBhardwaj56
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
chanes7
 
Top five deadliest dog breeds in America
Top five deadliest dog breeds in AmericaTop five deadliest dog breeds in America
Top five deadliest dog breeds in America
Bisnar Chase Personal Injury Attorneys
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
Priyankaranawat4
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
TechSoup
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
Celine George
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 
Assessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptxAssessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptx
Kavitha Krishnan
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
WaniBasim
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
Celine George
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
heathfieldcps1
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
thanhdowork
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 

Recently uploaded (20)

PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
 
Top five deadliest dog breeds in America
Top five deadliest dog breeds in AmericaTop five deadliest dog breeds in America
Top five deadliest dog breeds in America
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 
Assessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptxAssessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptx
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 

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.