close

Вход

Забыли?

вход по аккаунту

код для вставкиСкачать
CSE/Math 1560:
Introduction to Computing for
Mathematics and Statistics
Winter 2011
Suprakash Datta
[email protected]
Office: CSEB 3043
Phone: 416-736-2100 ext 77875
Course page: http://www.cse.yorku.ca/course/1560
3/17/2015
Math/CSE 1560, Winter 2011
1
Announcements
This week’s lab will also be a lightweight one.
You will work individually.
You can finish lab 1 without penalty. After this week, you
will have to finish each lab within the lab hours.
It is your responsibility to know which lab section you are
enrolled in and show up at the correct time.
3/17/2015
Math/CSE 1560, Winter 2011
2
Last class
Basic Maple commands and syntax
Today:
1. Some programming principles
2. More about solving equations and
3. Plotting graphs.
3/17/2015
Math/CSE 1560, Winter 2011
3
Programming principles
 Programs have to be readable, for




Debugging
Extending functionality
Maintenance
Evaluation
 Programming style includes directions for
creating code that is easier to read.
 Typical software companies spend much
more effort debugging and maintaining
software than in developing it.
3/17/2015
Math/CSE 1560, Winter 2011
4
Programming principle 1
Variable names
• Good mnemonic value
• Low ambiguity
Examples:
Bad names: i,j,x,y
Better names: iter_num, current_value,
x_squared
3/17/2015
Math/CSE 1560, Winter 2011
5
Programming principle 2
Comments (“documentation”)
• Meant to help the reader understand code
• Tradeoff between verbosity and readability
• Reader should not have to spend hours trying to figure
out what the programmer wants to do
Use to
• describe the problem being solved
• Overall idea of the solution
• Idea behind each major piece
• Any segment that may not be obvious
3/17/2015
Math/CSE 1560, Winter 2011
6
Verbosity vs readability
Bad example:
x:=x+1; # add one to the current value of x
Better example:
#find the number of zeroes in n!, n<=100
3/17/2015
Math/CSE 1560, Winter 2011
7
A middle school problem
Q: How many zeroes are there at the end
of 100!
3/17/2015
Math/CSE 1560, Winter 2011
8
Plotting graphs
Basic command:
>plot(func,x=a..b);
E.g. plot(x^2,x=0..3);
3/17/2015
Math/CSE 1560, Winter 2011
9
Multiple graphs
> plot([func1,func2,func3],x=a..b);
Example:
plot([3*x^4-5*x,10*sin(x)10,5*cos(x)+3*exp(x)],x=-2..2);
What’s missing?
3/17/2015
Math/CSE 1560, Winter 2011
10
A better plot
> plot(15+cos(x),x=0..4*Pi,labels = [“day”,”price”],title =
“Daily price of Maple syrup”);
3/17/2015
Math/CSE 1560, Winter 2011
11
More plot options
color=“Red”
thickness = 3
More at ?plot[options]
3/17/2015
Math/CSE 1560, Winter 2011
12
More on solving equations
> solve(3*x+2 = 11,x);
Check your answer:
> eval(3*x+2,x=3)
OR
> evalb(eval(3*x+2=11,x=3));
3/17/2015
Math/CSE 1560, Winter 2011
13
More on solving equations - 2
> solve(x^2-2 = 0,x);
Simplify:
> evalf(%) ;
BUT
> simplify(%) does not work!
3/17/2015
Math/CSE 1560, Winter 2011
14
More on solving equations - 3
> solve({3*x-2*y = 0,4*x+5*y=10},{x,y});
Simplify:
> evalf(%) ;
Notice:
solve(x^5-x^3=1,x);
RootOf(_Z^5-_Z^3-1, index = 1), RootOf(_Z^5-_Z^3-1, index = 2),
RootOf(_Z^5-_Z^3-1, index = 3), RootOf(_Z^5-_Z^3-1, index = 4),
RootOf(_Z^5-_Z^3-1, index = 5)
solve(x^5-x^3=1.0,x);
1.236505703, .3407948662+.7854231030*I, .9590477179+.4283659562*I, -.9590477179-.4283659562*I,
.3407948662-.7854231030*I
3/17/2015
Math/CSE 1560, Winter 2011
15
More on solving equations - 4
In general use fsolve for numerical solutions.
fsolve(x^5-x^3=1,x);
1.236505703
fsolve(x^5-x^3=1,x,complex,maxsols=5);
To access individual solutions:
Sols:= fsolve(x^5-x^3=1,x,complex,maxsols=5);
Sols[3];
3/17/2015
Math/CSE 1560, Winter 2011
16
Steps for solving equations
> solve(exp(x^2)-50*x^2+3*x = 0, x);
Warning, solutions may have been lost
> solve(exp(x^2)-50*x^2+3*x = 0., x);
Warning, solutions may have been lost
> fsolve(exp(x^2)-50*x^2+3*x = 0, x);
-0.1154942111
3/17/2015
Math/CSE 1560, Winter 2011
17
Solutions within a range
> fsolve(sin(x)-x=0,x,-Pi..Pi);
Can do the same for solving several
equations simultaneously.
3/17/2015
Math/CSE 1560, Winter 2011
18
1/--страниц
Пожаловаться на содержимое документа