Open navigation menu
Close suggestions
Search
Search
en
Change Language
Upload
Sign in
Sign in
Download free for days
0 ratings
0% found this document useful (0 votes)
3 views
Unit - Three - Lecture Notes - Programming With MATLAB
These notes cover matlab programming
Uploaded by
bed-com-45-22
AI-enhanced title
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
Download now
Download
Save Unit_Three_Lecture Notes_Programming with MATLAB For Later
Download
Save
Save Unit_Three_Lecture Notes_Programming with MATLAB For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
0 ratings
0% found this document useful (0 votes)
3 views
Unit - Three - Lecture Notes - Programming With MATLAB
These notes cover matlab programming
Uploaded by
bed-com-45-22
AI-enhanced title
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
Download now
Download
Save Unit_Three_Lecture Notes_Programming with MATLAB For Later
Carousel Previous
Carousel Next
Save
Save Unit_Three_Lecture Notes_Programming with MATLAB For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
Download now
Download
You are on page 1
/ 8
Search
Fullscreen
Unit 3: Programming with MATLAB 0 Introduction Algorithms are the main product of numerical analysis. A mathematical algorithm is a formal procedure describing an ordered sequence of operations to be performed a finite number of times. Like and division, as well as programming constructs like for, while, and if. Therefore, in this unit, our pes, algorithms form the basic building blocks of addition, subtraction, multiplication, primary concerns for programming in MATLAB will be three fold: to give students basic knowledge of MATLAB programming, to prepare students for other courses or modules where MATLAB js used, and to give you an insight into state-of-the-art tool for technical computation and visualization. Q Unit Objectives On successful completion of the Unit, students should be able to: Explain the basic concepts of flow control in MATLAB. Write MATLAB codes Execute function files Plot functions Relate MATLAB codes to other programming environments tu Key Terms As you go through this unit, ensure that you understand the key terms or phrases used in this unit as listed below: Statement Code Run Loop Function Plot 51co Flow Control ‘What is key to writing and executing MATLAB programs is the ability to understand the flow control. By this we mean a group of statements which form a basic building block of each programming language, not only in MATLAB. Thus section therefore explains the basic concepts of flow control in MATLAB. The topics include * Ifelse statement + Switch and case statement * For loop © While loop 1. The If-else statement This kind of logical statement evaluates a logical expression and executes a group of statements when the expression is true. Remember the TRUTH table of an “If...then...” statement from College Algebra module. The options elseif and else keywords provide for the execution of alternate groups of statements. An end keyword, which matches with the if, terminates the last group of statements. Then the groups of statements are delineated by the four keywords with no braces or brackets involved. Basically we have a structure as if..condition 1 % is true % execute these commands % execuite these commands elseif.....condition n % is true % execute these commands else.....otherwise % the default % execute these commands 52For example, if on the command window, we write a MATLAB code of “if-else” statement for executing an n xn matrix under some unspecified value of n, then we get an error “Undefined function or variable 'n' >> ifn>2 Meeye(n) elseif n<2 Meones(n) else Mzzeros(n) end Undefined function or variable 'n’ However, if we write same lines of code, but by beginning with specifying the value of n, first, then we get the result upon the TRUE condition as >> if m2 ye(n) | elseif n<2 | Mzones(n) else | M=zeros(n) | end | m= 10000 | 01000 | 00100 00010 00001 Here, we see that we get a 5 x 5 identity matrix, executed by the line of code “M=eye(n)” since the condition “ifn > 2” was TRUE given that n = 5. All other statements” conditions are FALSE and therefore cannot be executed. To see this, suppose n = 2, we get 53[o> nens >>ifm2 | Meeye(n) | elseifne2 Mzones(n) else M=zerostn) end Mz ° ° 2. Switch and case statement The switch-case statement executes groups of statements based on the value of a variable or expression. The keywords case and otherwise delineate the groups. Only the first matching case is executed. There must always be an end to match the switch. The format is ‘Switch (input) case 1 case2 For example, using same code for “if-else” statements, we now write >>n=2; >> switch(n) case1 Meeye(n) case 2 Meones(n) case 3 M=zeros(n) 543. For loop Key to running a sequence of steps ina MATLAB code is the “or...loop” statement, The for loop repeats a group of statements a fixed, predetermined number of times. A matching end delineates the statements. Its basic structure is (for ...X=array % execute these statements For example >> n=; >> for {m}=rank(magiclm)); end > 12335 Or another example 234567 55.4, Do While loop Another kind of important executable programming statement is the “Do...while...loop”. The while loop repeats a group of statements an indefinite number of times under control of a logical condition. A matching end delineates the statements. Therefore, the basic structure of.a while loop appears as while...expression x is true i | doxerecute these statements | ena For example, Bee >> ile p> | pot, | r=2er0s(p) end 5. Other flow structures a) The break statement. A while loop can be terminated with the break statement, which passes control to the first statement after the corresponding end. The break statement can also be used to exit a for loop. 56b) The continue statement can also be used to exit a for loop to pass immediately to the next iteration of the loop, skipping the remaining statements in the loop. ©) Other control statements include return, continue, switch, etc. Writing programs in MATLAB is supported by six relational and three logical operators as presented in Table 6 and Table 7, respectively. Table 6: MATLAB Relational Operators Expression ‘Symbol Less than Less than or equal to Greater than. Greater than or equal to Equal to Not equal to Table 7: MATLAB Logicel Operators Expression ‘Symbol not and & or i z 57Activity 3.a a) Consider the following system of linear equations x+2y432=1 3x+3yt4z=1 2x-+3y +32=2 a, Use the MATLAB editor to create a script file called exercise .m b. Hence, use the script file to solve for x using A\b method. b) Use a MATLAB script file called exercise2.m to plot the following cosine functions, yy = 2cosx , yz = cosx, and ys = 0.5 cos x, in the interval 0 < x < 2n. ¢) Consider the following quadratic equation y = 2x? + 3x — 4. a. Express it in the form of ax? + bx + c = 0 and deduce the values of a, b and c. b. Hence, write an if...else statement to ctassify the type of roots using discri 4) Write a for...loop statements which form the 5-by-5 symmetric matrix A with (i,j) element i/j for j > i. ©) Discuss the following while...loop and understand the output. 58
You might also like
The Subtle Art of Not Giving a F*ck: A Counterintuitive Approach to Living a Good Life
From Everand
The Subtle Art of Not Giving a F*ck: A Counterintuitive Approach to Living a Good Life
Mark Manson
4/5 (6127)
Principles: Life and Work
From Everand
Principles: Life and Work
Ray Dalio
4/5 (627)
The Gifts of Imperfection: Let Go of Who You Think You're Supposed to Be and Embrace Who You Are
From Everand
The Gifts of Imperfection: Let Go of Who You Think You're Supposed to Be and Embrace Who You Are
Brené Brown
4/5 (1148)
Never Split the Difference: Negotiating As If Your Life Depended On It
From Everand
Never Split the Difference: Negotiating As If Your Life Depended On It
Chris Voss
4.5/5 (932)
The Glass Castle: A Memoir
From Everand
The Glass Castle: A Memoir
Jeannette Walls
4/5 (8215)
Grit: The Power of Passion and Perseverance
From Everand
Grit: The Power of Passion and Perseverance
Angela Duckworth
4/5 (631)
Sing, Unburied, Sing: A Novel
From Everand
Sing, Unburied, Sing: A Novel
Jesmyn Ward
4/5 (1253)
The Perks of Being a Wallflower
From Everand
The Perks of Being a Wallflower
Stephen Chbosky
4/5 (8365)
Shoe Dog: A Memoir by the Creator of Nike
From Everand
Shoe Dog: A Memoir by the Creator of Nike
Phil Knight
4.5/5 (860)
Her Body and Other Parties: Stories
From Everand
Her Body and Other Parties: Stories
Carmen Maria Machado
4/5 (877)
The Hard Thing About Hard Things: Building a Business When There Are No Easy Answers
From Everand
The Hard Thing About Hard Things: Building a Business When There Are No Easy Answers
Ben Horowitz
4.5/5 (361)
Hidden Figures: The American Dream and the Untold Story of the Black Women Mathematicians Who Helped Win the Space Race
From Everand
Hidden Figures: The American Dream and the Untold Story of the Black Women Mathematicians Who Helped Win the Space Race
Margot Lee Shetterly
4/5 (954)
Steve Jobs
From Everand
Steve Jobs
Walter Isaacson
4/5 (2923)
Elon Musk: Tesla, SpaceX, and the Quest for a Fantastic Future
From Everand
Elon Musk: Tesla, SpaceX, and the Quest for a Fantastic Future
Ashlee Vance
4.5/5 (484)
The Emperor of All Maladies: A Biography of Cancer
From Everand
The Emperor of All Maladies: A Biography of Cancer
Siddhartha Mukherjee
4.5/5 (277)
A Man Called Ove: A Novel
From Everand
A Man Called Ove: A Novel
Fredrik Backman
4.5/5 (4972)
Angela's Ashes: A Memoir
From Everand
Angela's Ashes: A Memoir
Frank McCourt
4.5/5 (444)
Brooklyn: A Novel
From Everand
Brooklyn: A Novel
Colm Tóibín
3.5/5 (2061)
The Art of Racing in the Rain: A Novel
From Everand
The Art of Racing in the Rain: A Novel
Garth Stein
4/5 (4281)
The Yellow House: A Memoir (2019 National Book Award Winner)
From Everand
The Yellow House: A Memoir (2019 National Book Award Winner)
Sarah M. Broom
4/5 (100)
The Little Book of Hygge: Danish Secrets to Happy Living
From Everand
The Little Book of Hygge: Danish Secrets to Happy Living
Meik Wiking
3.5/5 (447)
The World Is Flat 3.0: A Brief History of the Twenty-first Century
From Everand
The World Is Flat 3.0: A Brief History of the Twenty-first Century
Thomas L. Friedman
3.5/5 (2283)
Devil in the Grove: Thurgood Marshall, the Groveland Boys, and the Dawn of a New America
From Everand
Devil in the Grove: Thurgood Marshall, the Groveland Boys, and the Dawn of a New America
Gilbert King
4.5/5 (278)
Yes Please
From Everand
Yes Please
Amy Poehler
4/5 (1987)
Bad Feminist: Essays
From Everand
Bad Feminist: Essays
Roxane Gay
4/5 (1068)
The Outsider: A Novel
From Everand
The Outsider: A Novel
Stephen King
4/5 (1993)
The Woman in Cabin 10
From Everand
The Woman in Cabin 10
Ruth Ware
3.5/5 (2641)
A Tree Grows in Brooklyn
From Everand
A Tree Grows in Brooklyn
Betty Smith
4.5/5 (1936)
The Sympathizer: A Novel (Pulitzer Prize for Fiction)
From Everand
The Sympathizer: A Novel (Pulitzer Prize for Fiction)
Viet Thanh Nguyen
4.5/5 (125)
A Heartbreaking Work Of Staggering Genius: A Memoir Based on a True Story
From Everand
A Heartbreaking Work Of Staggering Genius: A Memoir Based on a True Story
Dave Eggers
3.5/5 (692)
Team of Rivals: The Political Genius of Abraham Lincoln
From Everand
Team of Rivals: The Political Genius of Abraham Lincoln
Doris Kearns Goodwin
4.5/5 (1912)
Wolf Hall: A Novel
From Everand
Wolf Hall: A Novel
Hilary Mantel
4/5 (4074)
On Fire: The (Burning) Case for a Green New Deal
From Everand
On Fire: The (Burning) Case for a Green New Deal
Naomi Klein
4/5 (75)
Fear: Trump in the White House
From Everand
Fear: Trump in the White House
Bob Woodward
3.5/5 (830)
Rise of ISIS: A Threat We Can't Ignore
From Everand
Rise of ISIS: A Threat We Can't Ignore
Jay Sekulow
3.5/5 (143)
Manhattan Beach: A Novel
From Everand
Manhattan Beach: A Novel
Jennifer Egan
3.5/5 (901)
John Adams
From Everand
John Adams
David McCullough
4.5/5 (2542)
The Light Between Oceans: A Novel
From Everand
The Light Between Oceans: A Novel
M L Stedman
4.5/5 (790)
The Unwinding: An Inner History of the New America
From Everand
The Unwinding: An Inner History of the New America
George Packer
4/5 (45)
Little Women
From Everand
Little Women
Louisa May Alcott
4/5 (105)
The Constant Gardener: A Novel
From Everand
The Constant Gardener: A Novel
John le Carré
3.5/5 (109)
Related titles
Click to expand Related Titles
Carousel Previous
Carousel Next
The Subtle Art of Not Giving a F*ck: A Counterintuitive Approach to Living a Good Life
From Everand
The Subtle Art of Not Giving a F*ck: A Counterintuitive Approach to Living a Good Life
Principles: Life and Work
From Everand
Principles: Life and Work
The Gifts of Imperfection: Let Go of Who You Think You're Supposed to Be and Embrace Who You Are
From Everand
The Gifts of Imperfection: Let Go of Who You Think You're Supposed to Be and Embrace Who You Are
Never Split the Difference: Negotiating As If Your Life Depended On It
From Everand
Never Split the Difference: Negotiating As If Your Life Depended On It
The Glass Castle: A Memoir
From Everand
The Glass Castle: A Memoir
Grit: The Power of Passion and Perseverance
From Everand
Grit: The Power of Passion and Perseverance
Sing, Unburied, Sing: A Novel
From Everand
Sing, Unburied, Sing: A Novel
The Perks of Being a Wallflower
From Everand
The Perks of Being a Wallflower
Shoe Dog: A Memoir by the Creator of Nike
From Everand
Shoe Dog: A Memoir by the Creator of Nike
Her Body and Other Parties: Stories
From Everand
Her Body and Other Parties: Stories
The Hard Thing About Hard Things: Building a Business When There Are No Easy Answers
From Everand
The Hard Thing About Hard Things: Building a Business When There Are No Easy Answers
Hidden Figures: The American Dream and the Untold Story of the Black Women Mathematicians Who Helped Win the Space Race
From Everand
Hidden Figures: The American Dream and the Untold Story of the Black Women Mathematicians Who Helped Win the Space Race
Steve Jobs
From Everand
Steve Jobs
Elon Musk: Tesla, SpaceX, and the Quest for a Fantastic Future
From Everand
Elon Musk: Tesla, SpaceX, and the Quest for a Fantastic Future
The Emperor of All Maladies: A Biography of Cancer
From Everand
The Emperor of All Maladies: A Biography of Cancer
A Man Called Ove: A Novel
From Everand
A Man Called Ove: A Novel
Angela's Ashes: A Memoir
From Everand
Angela's Ashes: A Memoir
Brooklyn: A Novel
From Everand
Brooklyn: A Novel
The Art of Racing in the Rain: A Novel
From Everand
The Art of Racing in the Rain: A Novel
The Yellow House: A Memoir (2019 National Book Award Winner)
From Everand
The Yellow House: A Memoir (2019 National Book Award Winner)
The Little Book of Hygge: Danish Secrets to Happy Living
From Everand
The Little Book of Hygge: Danish Secrets to Happy Living
The World Is Flat 3.0: A Brief History of the Twenty-first Century
From Everand
The World Is Flat 3.0: A Brief History of the Twenty-first Century
Devil in the Grove: Thurgood Marshall, the Groveland Boys, and the Dawn of a New America
From Everand
Devil in the Grove: Thurgood Marshall, the Groveland Boys, and the Dawn of a New America
Yes Please
From Everand
Yes Please
Bad Feminist: Essays
From Everand
Bad Feminist: Essays
The Outsider: A Novel
From Everand
The Outsider: A Novel
The Woman in Cabin 10
From Everand
The Woman in Cabin 10
A Tree Grows in Brooklyn
From Everand
A Tree Grows in Brooklyn
The Sympathizer: A Novel (Pulitzer Prize for Fiction)
From Everand
The Sympathizer: A Novel (Pulitzer Prize for Fiction)
A Heartbreaking Work Of Staggering Genius: A Memoir Based on a True Story
From Everand
A Heartbreaking Work Of Staggering Genius: A Memoir Based on a True Story
Team of Rivals: The Political Genius of Abraham Lincoln
From Everand
Team of Rivals: The Political Genius of Abraham Lincoln
Wolf Hall: A Novel
From Everand
Wolf Hall: A Novel
On Fire: The (Burning) Case for a Green New Deal
From Everand
On Fire: The (Burning) Case for a Green New Deal
Fear: Trump in the White House
From Everand
Fear: Trump in the White House
Rise of ISIS: A Threat We Can't Ignore
From Everand
Rise of ISIS: A Threat We Can't Ignore
Manhattan Beach: A Novel
From Everand
Manhattan Beach: A Novel
John Adams
From Everand
John Adams
The Light Between Oceans: A Novel
From Everand
The Light Between Oceans: A Novel
The Unwinding: An Inner History of the New America
From Everand
The Unwinding: An Inner History of the New America
Little Women
From Everand
Little Women
The Constant Gardener: A Novel
From Everand
The Constant Gardener: A Novel