- Lab.-6
- Functions
- For Loop vs While Loop
- CSC 201 Lecture 4
- Java Script (1)
- C++ Tutorial
- u Script Tutorial
- 00 Iterations
- introduction to lists
- Bahasa Arduino
- doloop
- dsa ch2 priliminaries
- MELJUN CORTES C++ Demo MAY 7 2011
- Exp 5
- Lecture 09
- Sample Cprog
- csec mayjune2012 informationtechonology sr 1
- Bash Notes for Professionals
- uC Session 00 - Intro to C Programming
- Java for ABAP Programmers 8
- ICSE Class 10 Sample Paper 4
- OOP Lectures
- CSEC MayJune2012 InformationTechonology.sr
- Bash Notes for Professionals
- Control Break Processing_7
- cst231 final
- IntroductionTI-85-Progm
- Chapter 05 Control
- Computer Programming Data Structures
- C Programming Revised - An Exam Guide
- BHT of Ammonia
- 1-s2.0-S0017931014007030-main
- Phd Topics 2013 Dec Master File
- ZnO Ethylene Glycol
- Surfactant Additives
- 576-581
- IC ENGINE
- Jain Insurance Brokers Pvt Ltd
- 2131905
- ts dia
- ashrae
- 3
- Physics of Racing Series - Brian Beckman
- PhD Sample Pape81113 (1)
- Factories Act 1948 - Published on NATLEX
- R22,R125 and R32 performance
- refrigerant perfomance
- Collins Paul Noble Italian
- 10 Questions About Hinduism

February 14

Announcements

Announcements HW 1 is graded. Solution will be posted on the web site soon.

Announcements

Announcements HW 1 is graded. Solution will be posted on the web site soon. HW 2 postponed to Feb. 25 :

Iterations, for block

Iterations,loops We want to learn how to perform hundreds and thousands of operations. The way to do it is to repeat a similar command many times. Such a construction is called a ’loop’. for Here is the syntax to use for loop. for i=1:4 Comands... end Commands... will be executed for i=1,2,3,4.

More on for

Caution It’s not as simple as it seems. In the homework, you will need to perform the same operation many times. Usually, you will need to perform diﬀerent operations and adjust the result between the iterations of the loop. (and before you start the loop).

Example - sum of squares

**Example Write a function function s=sumsquares(n) which will have an input of integer number n and computes
**

n

s=

k=1

k2 ,

using the for loop.

Example - sum of squares

**Example Write a function function s=sumsquares(n) which will have an input of integer number n and computes
**

n

s=

k=1

k2 ,

using the for loop. This is just for the sake of exercise since sum( (1:n).^ 2) achieves the same result.

Designing our program

Design dilemmas for block should be thought as implementing an inductive procedure (i.e. something that mathematical induction works on) , you achieve a partial result in i − 1 steps and do the i-th step to get to the next partial result.

Designing our program

Design dilemmas for block should be thought as implementing an inductive procedure (i.e. something that mathematical induction works on) , you achieve a partial result in i − 1 steps and do the i-th step to get to the next partial result. In this case denote si =

i 2 k=1 k

then si+1 = si + i2 .

A correct way to initialize it is s0 = 0.

Sum squares matlab code

sumsquares.m function s=sumsquare(n) % sumsquare sums the squares of integer numbers % up to n partialsum=0; for i=1:n partialsum=partialsum+i^2; end s=partialsum; end

Newton with for loop

Newton with for loop function x=fnNewt3(x0) xold=x0; for i=1:4 xnew=xold/2+5/(2*xold); xold=xnew; end x=xnew; end

’while’ block

while syntax while condition commands end will repeat commands while the logical condition holds.

’while’ block

while syntax while condition commands end will repeat commands while the logical condition holds. while what should really happen Think how to initialize the variables before you enter the loop. Before the end of the iteration, think how to advance all the variables for the next iteration. For the while loop, make sure that the termination condition gets updated or you won’t exit the loop.

Sum squares with while

sumsquareswhile.m function s=sumsquarewhile(n) % sumsquare sums the squares of integer numbers % up to n partialsum=0; i=0; while (i<=n) partialsum=partialsum+i^2; i=i+1; end s=partialsum; end

Blocks and indentation

Blocks (of code) in Matlab if, for, while start a block of code in Matlab. Each block has to be terminated with end. You can nest blocks within a block (you will have to for the HW). The last open (unended) block gets terminated ﬁrst. A good practice is to indent the code in the block to the right. That way, you it is easier to know in which block you are exactly. MATLAB editor helps you do that both interactively and using ’smart indent’ in the right-click menu. Observe the indentation rules for the homework!

Exercises

Exercise 1 Rewrite your Newton function to accept as inputs the initial guess and the number of iterations it should perform (and output the result). Exercise 2

−10 . Rewrite your Newton function to run until |x2 n − 5| ≤ 10 Additionaly, try to output the number of iterations it took.

More on the next slide

Exercises (continued)

Exercise 3 Design a program that computes n! Exercise 4 Design a program that computes

n 2 k=1 1/k .

**Exercise 5 The Fibonacci sequence Fn = Fn−1 + Fn−2 for n ≥ 2, where F0 = 0, F1 = 1, has the explicit formula 1 Fn = √ 5 √ 1+ 5 2
**

n

−

√ 1− 5 2

n

.

What could go wrong with programming this in directly? More on the next slide

Exercises (continued)

Exercise 5 (cont.) Write a function to compute Fibonacci numbers iteratively. The input should be n and the output Fn . Exercise 6 Write a function to compute Fibonacci numbers using the explicit formula and compare the results for large n.

- Lab.-6Uploaded byroxette
- FunctionsUploaded byvasantha_btech
- For Loop vs While LoopUploaded byKhalil Gaynor
- CSC 201 Lecture 4Uploaded bypavanil
- Java Script (1)Uploaded byShivam Dua
- C++ TutorialUploaded bynirmalthalassery
- u Script TutorialUploaded byAbhishek Kumar
- 00 IterationsUploaded byomarf_1729
- introduction to listsUploaded byapi-341915966
- Bahasa ArduinoUploaded byqurrotuayun
- doloopUploaded byppaulyni33
- dsa ch2 priliminariesUploaded byapi-394738731
- MELJUN CORTES C++ Demo MAY 7 2011Uploaded byMELJUN CORTES, MBA,MPA
- Exp 5Uploaded byistebreq
- Lecture 09Uploaded byZhichao Wang
- Sample CprogUploaded bystarirayako
- csec mayjune2012 informationtechonology sr 1Uploaded byapi-247871582
- Bash Notes for ProfessionalsUploaded byconrado
- uC Session 00 - Intro to C ProgrammingUploaded byRodz Gier Jr
- Java for ABAP Programmers 8Uploaded byAarón Villar
- ICSE Class 10 Sample Paper 4Uploaded byChirag
- OOP LecturesUploaded bycaspercastle
- CSEC MayJune2012 InformationTechonology.srUploaded bydextonslab7
- Bash Notes for ProfessionalsUploaded byPeter Ranieri
- Control Break Processing_7Uploaded byVikram Bigamudre
- cst231 finalUploaded byapi-384692750
- IntroductionTI-85-ProgmUploaded bysigilo
- Chapter 05 ControlUploaded bysatkm87
- Computer Programming Data StructuresUploaded byBeing Manish
- C Programming Revised - An Exam GuideUploaded bySiddhartha Sharma

- BHT of AmmoniaUploaded byVipin Nair
- 1-s2.0-S0017931014007030-mainUploaded byVipin Nair
- Phd Topics 2013 Dec Master FileUploaded byVipin Nair
- ZnO Ethylene GlycolUploaded byVipin Nair
- Surfactant AdditivesUploaded byVipin Nair
- 576-581Uploaded byVipin Nair
- IC ENGINEUploaded byVipin Nair
- Jain Insurance Brokers Pvt LtdUploaded byVipin Nair
- 2131905Uploaded byVipin Nair
- ts diaUploaded byVipin Nair
- ashraeUploaded byVipin Nair
- 3Uploaded byVipin Nair
- Physics of Racing Series - Brian BeckmanUploaded byVipin Nair
- PhD Sample Pape81113 (1)Uploaded byVipin Nair
- Factories Act 1948 - Published on NATLEXUploaded byVipin Nair
- R22,R125 and R32 performanceUploaded byVipin Nair
- refrigerant perfomanceUploaded byVipin Nair
- Collins Paul Noble ItalianUploaded byVipin Nair
- 10 Questions About HinduismUploaded byVipin Nair