Lecture 3 graphs, least squares ONE

Lecture 3 graphs, least squares
ONE-VARIABLE
FUNCTION
GRAPHS,
PLOTTING
POINTS
We want to plot four points: (0, 1), (1, 2), (2, , 4), (3, 3). Type (or
copy/paste) the vectors of x-coordinates and of y-coordinates into Scilab.
Type the remaining lines.
x =[0,1,2,3]
y =[1,2,4,3]
plot(x,y)
//Scilab chooses the line style
clf
//Clears the graph window
plot(x,y,’r-’)
// red -’s
plot(x,y+1,’g-’) // green -’s
In the graph window, click Edit/Figure Properties. Click Axes(1),
The X tab shows.
In Text, wait, enter x-axis between quotes.
In Data bounds:, decrease lower by 1, increase upper by 1. <Enter >
Click the Y tab. In Label:, enter ’y-axis’ between quotes. In Data
bounds:, decrease lower by 1, increase upper by 1. <Enter >
Click the Title tab, in Text, enter My graph <Enter >.
Click Compound(1), wait, then Polyline(1)
Make the upper line red (Foreground, slide control)
with solid line of size 2(Line=solid,2),
with circle data points (Mark style=dot, size=6).
Click Compound(2), wait, then Polyline(2)
Make the lower y line dashed blue with square data points.
Check Line Mode, Line=dash,2, Mark style=square,6.
It should look like this if you set line size to 2.
GRAPHING FUNCTIONS
Close the graph window and Polyline editor. OpenSciNotes.
In Matlab sin−1 , cos−1 , tan−1 are written, asin, acos, atan. To graph
sin(x) type the following in Scilab.
x=-%pi:0.01:%pi;
y=sin(x);
clf;plot(x,y)
Now open SciNotes and type this more compact single line.
clf;plot(-%pi:0.01:%pi,sin)
c3.1. In SciNotes, select File/New, copy/paste template lines. Save as
c3.1.
Plot arctan from -10 to 10 with one line of Scilab code.
//c3.1 Plot arctan from -10 to 10 with one line of code.
1
Graph user-defined functions in the same way.
In SciNotes, select File/New, copy/paste, execute <ctrl-l>the lines below.
function y=f(x);
y= sin(x**2);
endfunction
clf;plot(-%pi:0.01:%pi,f)
Open Edit/Figure Properties, click Axes(1), wait, Set X-axis
Location=middle, Grid color = 0. Set Y-axis Location=middle,
Grid color = 0.
In Scilab we write intg(a,b,f) for
defined function..
Rb
a
f (x)dx where f must be a user-
Rπ
Example: Find −π (1 − cos2 (x))dx
function y=g(x)
y=1-(cos(x))**2;
endfunction
intg(-%pi,%pi,g)
c3.2 In SciNotes, select File/New, copy/paste template lines. Save as
c3.2. // c3.2(2) Define the upper semicircle function.
// Graph the semicircle
// Find the area of the unit circle.
Calculate the area of the unit circle x2 + y 2 = 1.
The radius is 1, so the area is πr2 = π ∗√
12 = π ' 3.14.
In x2 + y 2 = 1, solving for y√gives y = ± 1 − x2 . So the equation of the
upper semicircle is h(x) = 1 − x2 .
Define the function h(x).
Plot it between x =-1 and 1.
Use the integral to get the upper semicircle area.
Double it to get the unit circle area.
AREAS AND INTEGRALS
The integral of a function f and between values x = a and x = b is the
area between
the graph of f and the x-axis, from x = a to x = b. It is
Rb
written a f (x)dx.
TWO-VARIABLES FUNCTION GRAPHS
We want topdraw a contour graph and a 3D graph of the upper hemisphere z = 1 − x2 − y 2 .
clf
//clears graph window.
x= -2:.01:2;
// need the ;
y= -2:.01:2;
//need the ;
for i=1:length(x); for j=1:length(y);
z(i,j)= sqrt(1-x(i)^2-y(j)^2);
//need the ;
end; end;
contour(x,y,z,20)
//20 = # of levels
2
In this example, two for loops are interlocked with each other. The
first one (for i=1:length(x)..) is used to go all over the points
on the abcissa. For each of these points, we use another loop (for
j=1:length(j)..) to go all over the points on the ordinate. Thus,
we go over all the points (x(i),y(j)) on the grid. For each of these
points, we can calculate the corresponding value z(i,j).
FINDING A LINE WHICH BEST FITS GIVEN DATA
POINTS.
Scilab writes polynomials backwards (in ascending power order). So
it writes 2 − x + 4x2 instead of 4x2 − x + 2. It encodes a polynomial
with the vector of its coeffients. Hence p = 2 − x + 4x2 is encoded with
the vector p=[2, -1, 4]. Hence p(1)=2,p(2)=-1,p(3)=4. We define
polyfit (it is built into Matlab) which finds the coefficient vector of a
degree n polynomial which best fits the data.
function p = polyfit(x, y, n) //n=degree
m=length(x);
aa = zeros(n+1, m)
aa(1,:) = ones(1,m)
for k = 2:n+1
aa(k,:) = x.^(k-1)
end
p = y/aa
endfunction
c3.3 In SciNotes, select File/New. Save as c3.3. Write 7 lines in SciNotes
which generate a nice contour plot. of z = xy. Set the number of contour levels to 15. Include the needed ’;’. //c3.3 Change to make a
contour plot of z=xy.
//T
3D GRAPHS
clf
//clears graph window.
[x,y]=meshgrid(-1:.1:1); //need the ;
z=sqrt(1-x.^2-y.^2);
//x, y are matrices, need the ;
mesh(x,y,z)
A line is a polynomial y = b + mx of degree n = 1. Hence use
polyfit(x,y,1) in homework/classwork which asks for a best-fit line.
polyfit(x,y,2) is for fitting parabolas (like 2−x+4x2 ) instead of lines.
Notice that the outputs x and y are respectively the matrices of the
abcissas and ordinates of the points in the grid defined by the cross
product [−1 : .1 : 1]×[−1 : .1 : 1]. Thus, x.^2 and y.^2- are respectively
the matrices of the squares of the abcissas and ordinates in this grid.
c3.5 Open a new file, Copy/Paste/Save code as c3.5.
Find the line y = b + mx which best fits the following data:
c3.4 In SciNotes, select File/New,Save as c3.4.
Write 4 SciNote lines which generate a 3D graph of z = xy.
//c3.4(2) Change to make a 3D graph of z=xy.
(0, 1), (1, 2), (2, 2), (2, 3), (3, 3)
Include the needed ’;’ ! Should get a saddle.
If you get a flat surface, you wrote z=x*y, instead of z=x.*y. If you get
a partially flat surface or if two quadrants are blank, you probably wrote
z=
p sqrt(x.*y). If you get a dome, you didnt change the formula from
1 − x2 − y 2 to xy.
Plot the points, plot the best-fit line and write its equation. Change the
x=, y= lines below to make them the x and y coordinates of the data
points above.
Copy/Paste the 9 lines of the polyfit function above into this
SciNotes window.
3
Copy/Paste the lines of code below after the polyfit code. Execute.
Finally, change the red characters to fit the data.
//c3.5(2) Scilab code for the best fit line for
// (0,1),(1,2),(2,2),(2,3),(3,3)
clf;
x=[0,0,7,3]
y=[1,3,3,7]
p=polyfit(x,y,1); b=p(1),m=p(2),
plot(x,y,’ro’)
plot(x,b+m*x,’b-’) // Enlarge the X and Y data bounds.
printf(’ Equation: y=%6.2f+%6.2fx ’,b,m)
Checksums (sum of their respective digits) for the correct coefficients
are 7 and 11.
If you get a polyfit undefined error, you didnt paste in the polyfit code
at the top.
c3.6 Open a new file, Copy/Paste/Save code as c3.6.
Format with one line per person (add a new line character \n). Make
phone numbers strings. Make the columns line up (give all the character
strings the same maximum length).
//mode(0); warning(’off’); //Reduce echoed verbiage, use
’;’
name=[’Tom’,’Dick’,’Harry’]
phone=[1234, 0222, 555-4321]
for i=1:3;
printf(’Name: %s Phone:%i ’,name(i),phone(i))
end
HOMEWORK 3 DUE BEFORE FRIDAY FEBRUARY 6TH’s
CLASS .
email to: [email protected] subject line: 190 h3(14)
h3.1(3), h3.2(2), h3.3(2), h3.4(2), h3.5(5)
Dont attach graphs.
In each problem, make clf the first line to clear the graph window
.
h3.1(3) Put in one SciNotes window. For the function f (x) = x3 − x
write a Scilab code which plots the graph of f between x = −1.5 and
x = 1.5 and finds the integral of f between x = −0.5 and x = 1.5.
Recall: integral below the x-axis is considered negative!
FORMATTED PRINTING
The printf command can print values of variables in a sentence or a
table with formatting of your own choosing.
%i
integer
%6i
integer (right-justified) i a field of 6 spaces
%f
decimal
%4.2f decimal field of 4 spaces with 2 decimal places
%s
character string.
%12s
string (right-justified) in a field of 12 spaces.
\n
makes a new line.
e3.1 Format the decimal to two decimal places.
for i=1:5
printf(’1/%i = %f \n’,i,1/i)
end
4
h3.2(2) Write 6 lines in SciNotes which generate a nice contour plot of
z = x2 − xy + y 2 .
h3.5(5). Write a function print matrix(a) which on a matrix a, prints
the matrix with column titles 1, 2, 3 at the top and row titles 1, 2 on
the left. Titles and numbers must be right-justified in their column. The
first column has fields of length 4, the remaining columns have width 6.
If a=[11 12 13; 23 22 23], the printout should be
1 2 3
1 11 12 13
2 21 21 23
Fill in the three blanks
// h3.5(5) Fill in the three blank lines
//h3.2(2) Code for contour plot of z=x**2-xy+y**2
// Should be round contours. -1 point for omitting the
needed ’;’
h3.3(2) Write 4 lines in SciNotes which generate a nice 3D graph of
z = x2 − xy + y 2 .
. // h3.3(2) 3D graph of z=x**2-xy+y**2
// Should be a bowl shaped valley. -1 point for omitting
// the needed ;
If you computer doesnt seem to be doing anything, check your Scilab
window, you may have forgotten the ; which causes a printing overflow
in Scilab requiring user input of n or y.
mode(0); warning(’off’);
function print matrix(a)
[n,m]=size(a) // n=number of rows, m=#columns
printf(’
’);
for j=1:m; printf(’%6i’,j); end;
printf(’\n’);
for i=1:n;
; //print row number
for j=1:m;
;//print ith matrix row entries
end
; //print a new line here
end
endfunction
a=[11 12; 21 22; 31 32; 41 42]; print matrix(a)
a=[11 12 13 14; 21 22 23 24]; print matrix(a)
h3.4(2) Copy/Paste the 9 lines of the polyfit function above into a new
SciNotes window. After the polyfit lines, write a Scilab code which
plots the following points, plots the best-fit line and writes its equation.
(0,1),(1,1),(2,2),(2,3),(3,2)
// h3.4(2)(a) Scilab code for the best fit line for
// (0,1),(1,1),(2,2),(2,3),(3,2)
// checksum ( sum of digits) for m = 5
5