Document 10382176

advertisement
avery_tutorial_wxmaxima_3.wxm
1 / 9
You can do some interesting sums in closed form using the sum function
and the "simpsum" suffix modifier.
First, let's add 5 to itself N times
(%i1) sum(5, i, 1, N);
(%o1) 5 N
Now add the integers from 1 to N. Use simpsum to make a closed form expression.
(%i2) sum(i, i, 1, N);
(%o2)
N
X
i
i=1
(%i3) %, simpsum;
%, factor;
(%o3)
(%o4)
2
N +N
2
N (N + 1 )
2
Let's do more complicated sums
(%i5) sum(i^2, i, 1, N), simpsum, factor;
sum(i^3, i, 1, N), simpsum, factor;
sum(i^4, i, 1, N), simpsum, factor;
sum(2*i-1, i, 1, N), simpsum, factor;
(%o5)
(%o6)
(%o7)
N (N + 1 ) (2 N + 1 )
6
N
( N + 1 )2
2
4
°
2
N (N + 1 ) (2 N + 1 ) 3 N + 3 N - 1
30
(%o8) N
Ñ
2
(%i9) sum(exp(i*x), i, 0, N), simpsum;
(%o9)
%e
x (N + 1 )
-1
x
%e - 1
(%i10) sum(sin(i*x), i, 0, 4), trigexpand, factor;
%, trigreduce, factor;
(%o11)
°
( ) 4 cos( x ) sin( x )2 + sin( x )2 - 4 cos( x )3 - 3 cos( x )2 - 2 cos( x ) - 1
sin( 4 x ) + sin( 3 x ) + sin( 2 x ) + sin( x )
(%o10) - sin x
Ñ
Let's do some interesting sums with inverse powers of integers. This is the definition
of the Riemann zeta function! Note how even powers give closed form expressions.
avery_tutorial_wxmaxima_3.wxm
2 / 9
(%i12) sum(1/i^2, i, 1, inf), simpsum;
sum(1/i^3, i, 1, inf), simpsum;
sum(1/i^4, i, 1, inf), simpsum;
sum(1/i^5, i, 1, inf), simpsum;
sum(1/i^6, i, 1, inf), simpsum;
sum(1/i^12, i, 1, inf), simpsum;
(%o12)
Ù2
6
( )
(%o13) ° 3
(%o14)
Ù4
90
( )
(%o15) ° 5
(%o16)
(%o17)
Ù6
945
691 Ù 12
638512875
Numerical integration is necessary for functions that do not have a closed form integrand.
We use the romberg method (successively halving the interval size).
First check an integral we already know and compare the answer to the exact one.
(%i18) integrate(sin(x)^2, x, -%pi, %pi);
romberg(sin(x)^2, x, -%pi, %pi);
% - float(%pi);
(%o18) Ù
(%o19) 3.14159265783776
(%o20) 4.2479664230654635 10
-9
(%i21) romberg(exp(-x), x, 0, 1);
% - float(1-1/%e);
(%o21) .6321205638903562
(%o22) 5.06179853587696 10
-9
Change tolerance for convergence to increase accuracy. The romberg algorithm converges whenever
1. absolute difference in successive iterations <= rombergabs OR
2. relative difference in successive iterations <= rombergtol OR
3. The number of iterations = rombergit
Because of floating point accuracy you don't want to make the tolerances too small because
convergence might become impossible.
Default values for rombergabs, rombergtol, rombergit. Since rombergabs is 0 by default,
only rombergabs and rombergit are used to determine convergence.
(%i23) float([rombergabs, rombergtol, rombergit]);
(%o23) [ 0.0 , 1. 10
-4
, 11.0 ]
Change the relative tolerance and do the integrals again. Note the improved accuracies.
(%i24) rombergtol : 1.e-8;
(%o24) 1. 10
-8
(%i25) romberg(sin(x)^2, x, -%pi, %pi);
% - float(%pi);
(%o25) 3.141592653588757
(%o26) - 1.036504215790046 10
-12
avery_tutorial_wxmaxima_3.wxm
3 / 9
(%i27) romberg(exp(-x), x, 0, 1);
% - float(1-1/%e);
(%o27) .6321205588290519
(%o28) 4.942712905631197 10
-13
Let's try plotting some functions. For this we need wxplot2d.
First, let's plot sin(x) from 0 - 4*pi
(%i29) wxplot2d(sin(x),[x,0,4*%pi]);
(%t29)
(%o29)
To plot more than 1 function, we use a list of expressions as the first argument
rather than just an expression. Colors are assigned by default.
(%i30) wxplot2d([sin(x)/x, x^2*exp(-x), 0.5*cos(x)^2], [x, 0, 10]);
plot2d: expression evaluates to non-numeric value somewhere in plotting range.
(%t30)
(%o30)
Same thing, but one can also control the colors of the three plots.
avery_tutorial_wxmaxima_3.wxm
(%i31) wxplot2d([sin(x)/x, x^2*exp(-x), 0.5*cos(x)^2], [x, 0, 10],
[color,black,red,green]);
plot2d: expression evaluates to non-numeric value somewhere in plotting range.
(%t31)
(%o31)
You can also specify the vertical range with a y range
(%i32) wxplot2d([sin(x)/x, x^2*exp(-x), 0.5*cos(x)^2], [x, 0, 10], [y, -1, 2]);
plot2d: expression evaluates to non-numeric value somewhere in plotting range.
(%t32)
(%o32)
You can also make parametric plots with the parametric keyword.
4 / 9
avery_tutorial_wxmaxima_3.wxm
(%i33) wxplot2d([parametric,sin(t),sin(2*t),[t,0,2*%pi]]);
(%t33)
(%o33)
The default number of points is too small in parametric plots.
Use the nticks variable to increase the number of points for better smoothness.
(%i34) wxplot2d([parametric,sin(t),sin(2*t),[t,0,2*%pi]], [nticks,100]);
(%t34)
(%o34)
You can increase the thickness of the line with styles.
5 / 9
avery_tutorial_wxmaxima_3.wxm
(%i35) wxplot2d([parametric,sin(t),sin(2*t),[t,0,2*%pi]], [nticks,100], [style,[lines,3]]);
(%t35)
(%o35)
(%i36) wxplot2d([sin(x)/x, x^2*exp(-x), 0.5*cos(x)^2], [x, 0, 10], [y, -1, 2],
[style,[lines,2]]);
plot2d: expression evaluates to non-numeric value somewhere in plotting range.
(%t36)
(%o36)
Let's make a 3-D plot, where color is proportional to z value.
Default grid is 30 x 30.
Default view is from angle angle (elevation = 60 degrees, azimuth = 30 degrees)
6 / 9
avery_tutorial_wxmaxima_3.wxm
(%i37) wxplot3d(1/(1+x^2+y^2), [x,-3,3], [y,-3,3])$
(%t37)
Make a high resolution grid 100 x 100
(%i38) wxplot3d(1/(1+x^2+y^2), [x,-3,3], [y,-3,3], [grid,100,100]);
(%t38)
(%o38)
We can turn off the color palette. We also increase the plot size to 800 x 600 pixels.
7 / 9
avery_tutorial_wxmaxima_3.wxm
(%i39) wxplot_size: [800,600];
wxplot3d(1/(1+x^2+y^2), [x,-3,3], [y,-3,3], [grid,100,100], [palette,false]);
(%o39) [ 800 , 600 ]
(%t40)
(%o40)
Look at plot from on top so that z axis is not visible and color becomes the only clue for z value.
We do this by changing the elevation and azimuth variables to 0 (i.e., plot seen from above).
I also add labels to the x and y axes and added a legend.
Unfortunately, the elevation, azimuth and mesh_lines_color settings have no effect here.
8 / 9
avery_tutorial_wxmaxima_3.wxm
(%i41) wxplot3d(1/(1+x^2+y^2), [x,-3,3], [y,-3,3], [elevation,0], [azimuth,0], [mesh_lines_color,false],
[grid,100,100], [xlabel, "x values"], [ylabel, "y values"],
[legend,"Lorentzian curve"]);
(%t41)
(%o41)
9 / 9
Download