180 likes | 190 Views
Suprakash D at ta datta@cse.yorku.ca Office: CSEB 3043 Phone: 416-736-2100 ext 77875 Course page: http://www.cse.yorku.ca/course/1560. CSE/Math 1560: Introduction to Computing for Mathematics and Statistics Winter 2011. Announcements. This week’s lab will also be a lightweight one.
E N D
Suprakash Datta datta@cse.yorku.ca Office: CSEB 3043 Phone: 416-736-2100 ext 77875 Course page: http://www.cse.yorku.ca/course/1560 CSE/Math 1560:Introduction to Computing for Mathematics and Statistics Winter 2011 Math/CSE 1560, Winter 2011
Math/CSE 1560, Winter 2011 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.
Math/CSE 1560, Winter 2011 Last class Basic Maple commands and syntax • Today: • Some programming principles • More about solving equations and • Plotting graphs.
Math/CSE 1560, Winter 2011 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.
Math/CSE 1560, Winter 2011 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
Math/CSE 1560, Winter 2011 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
Math/CSE 1560, Winter 2011 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
Math/CSE 1560, Winter 2011 A middle school problem • Q: How many zeroes are there at the end of 100!
Math/CSE 1560, Winter 2011 Plotting graphs • Basic command: >plot(func,x=a..b); E.g. plot(x^2,x=0..3);
Math/CSE 1560, Winter 2011 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?
Math/CSE 1560, Winter 2011 A better plot > plot(15+cos(x),x=0..4*Pi,labels = [“day”,”price”],title = “Daily price of Maple syrup”);
Math/CSE 1560, Winter 2011 More plot options • color=“Red” • thickness = 3 • More at ?plot[options]
Math/CSE 1560, Winter 2011 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));
Math/CSE 1560, Winter 2011 More on solving equations - 2 > solve(x^2-2 = 0,x); Simplify: > evalf(%) ; BUT > simplify(%) does not work!
Math/CSE 1560, Winter 2011 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
Math/CSE 1560, Winter 2011 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];
Math/CSE 1560, Winter 2011 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
Math/CSE 1560, Winter 2011 Solutions within a range > fsolve(sin(x)-x=0,x,-Pi..Pi); Can do the same for solving several equations simultaneously.