This preview shows page 1. Sign up to view the full content.
Unformatted text preview: A
Idea: Make a square with area A n༆ Real task: Make a sequence of increasingly square
rectangles, each with area A Insight Through Computing 2 1/28/14 Idea: How to make a rectangle “more square”?
n༆ W If a square and a rectangle both have area A … L
L1 = (L+W)/2
n༆ W1 = A/L1 then A is between the length and width of the
rectangle W1
L1
Insight Through Computing Repeat: A Matlab program to make “increasingly square” rectangles % The first rectangle...
L1 = A;
W1 = 1;
% The second rectangle...
L2 = (L1+W1)/2;
W2 = A/L2;
% The third rectangle...
L3 = (L2+W2)/2;
W3 = A/L3;
% and so on... W1
L1
L2 = (L1+W1)/2 W2 = A/L2 Lecture 1 9 Insight Through Computing Square Root Conclusions • Algorithm via simple geometry • Reduces to a simple formula • Looks complicated, but code is simple Roots Quadratic equation ax 2 + bx + c = 0 What is the formula for the roots? Insight Through Computing Insight Through Computing 3 1/28/14 Roots Roots Quadratic equation ax 2 + bx + c = 0 What is the formula for the roots? − b ± b 2 + 4ac
r=
2a • Let’s solve Insight Through Computing Insight Through Computing x 2 + 5x + 6 = 0
• Using Matlab Script % Quad1
% Solves
a
b
c
d
r1
r2 =
=
=
=
=
= Programming Basics Variables and Expressions Assignment Statements Built In Functions Script Execution Comments Keyboard Input Formatting Output x^2 + 5x + 6 = 0 1;
5;
6;
sqrt(b^2 4*a*c);
(b  d)/(2*a)
(b + d)/(2*a) Insight Through Computing Insight Through Computing Script A sequence of instructions.
The order of the instructions is
important.
A script is a program. Insight Through Computing Comments % Quad1
% Solves
a
b
c
d
r1
r2 =
=
=
=
=
= x^2 + 5x + 6 = 0 1;
5;
6;
sqrt(b^2  4*a*c);
(b  d)/(2*a)
(b + d)/(2*a) Insight Through Computing 4 1/28/14 Arithmetic Expressions Comments Begin with the “%” symbol. Goes to the
end of the line.
Facilitate the reading and understanding of
the script. Insight Through Computing % Quad1
% Solves
a
b
c
d
r1
r2 =
=
=
=
=
= 1;
5;
6;
sqrt(b^2  4*a*c);
(b  d)/(2*a)
(b + d)/(2*a) Insight Through Computing Arithmetic Expression Built In Functions % Quad1
% Solves A recipe that results in the production
of a number. Insight Through Computing a
b
c
d
r1
r2 =
=
=
=
=
= x^2 + 5x + 6 = 0 1;
5;
6;
sqrt(b^2  4*a*c);
(b  d)/(2*a)
(b + d)/(2*a) Insight Through Computing Built In Functions Variables % Quad1
% Solves These are “packagings” of more
advanced calculations.
Some examples: log, exp, sin, cos,… Insight Through Computing x^2 + 5x + 6 = 0 a
b
c
d
r1
r2 =
=
=
=
=
= x^2 + 5x + 6 = 0 1;
5;
6;
sqrt(b^2  4*a*c);
(b  d)/(2*a)
(b + d)/(2*a) Insight Through Computing 5 1/28/14 Assignment Statements Variables • A variable is a “box” that holds a numerical value % Quad1
% Solves • It has a name • The name must begin with a letter • Upper and lower cases are distinguished. Can...
View
Full
Document
This document was uploaded on 03/11/2014 for the course CSCI 004 at Brown.
 Spring '14
 Potter
 Algorithms

Click to edit the document details