Easy2.mws
solving two equations in two unknowns
loops and derivatives
restart; with(plots):
We illustrate the solution of two equations in two unknowns
s:=solve({3*x+4*y=3, 3*x-3*y=7},{x,y});
x;
After the solution, maple doesn't know what x is. x has not been 'assigned'
assign(s);
x;
Now maple knows x and y. (And they are no longer
free to be variables.)
Assigning a solution is important if something is to be done with x and
y later.
Next, we illustrate the use of a loop. Everything between the do and the od gets executed the specified number of times. Be sure to include the od at the end, or your whole maple session can get wrecked. This loop just adds the integers from 1 to 10. This can be packed on one line, but the choice of how much goes on one line is up to you.
h:=0:
for k from 1 to 10 do
h:=h+k: od:
h;
Here is a more complicated use of loops; a total charge of 1 nC in N even chunks from -1 m to +1m along the x-axis. We will plot along the y-axis.
First, we need a function for the potential, with q in nC. This is the potential due to a charge q at (a,0,0) at point (x,y,z).
V:=9*q/sqrt((x-a)^2+y^2+z^2);
Oops, x and y are committed, so we free them up and go again.
x:='x'; y:='y';
V:=9*q/sqrt((x-a)^2+y^2+z^2);
Now we break 1 nC into 6 pieces and place from -1 to 1, roughly.
I often forget the grammar of the loop, so I get reminded by asking for help on the for statement. One of the examples in the help shows going by 2's as I did below. Note that the subs command plays an important role.
?for
V6pcs:=0; # break charge into 6 chunks
for k from -5 to 5 by 2 do
V6pcs:=V6pcs+subs(q=1/6,a=k/6,x=0,z=0,V): # a runs from -2.5/3 to +2.5/3
along x-axis
od: V6pcs;
plot(V6pcs,y=0..4,title=` 6 pieces from -1 to 1`);
Now break the 1 nC into 12 pieces and spread them over the same range. Will V at the origin be bigger or smaller?
V12pcs:=0: # break charge into 12 chunks
for k from -11 to 11 by 2 do
V12pcs:=V12pcs+subs(q=1/12,a=k/12,x=0,z=0,V): # a runs from -5.5/6 to +5.5/6
along x-axis
od: V12pcs:
plot(V12pcs,y=0..2,title=`12 pieces from -1 to 1`);
The two graphs should differ a little at y=0 and then rapidly come together at larger values of y, because we have the same total charge spread over the same total distance.
plot({V6pcs,V12pcs},y=0..4,title=`6 and 12 chunks from -1 to 1`); # plot both together
Now for some derivatives.
First, we define a function, find its first derivative and where it is zero, then check the result with a plot.
z:=2*x^3*exp(-x); # function
dzdx:=diff(z,x); # derivative
solve(dzdx=0,x); # find where derivative is zero
plot(z,x=0..6); # plot and see that it agrees
Then we find the laplacian of the V function, then simplify it to show it's zero. Note the two different but equivalent ways of asking for a second derivative.
z:='z'; # free up z
laplacian:=diff(V,x,x)+diff(V,y$2)+diff(V,z,z);
simplify(laplacian);
z:=2*x^3*exp(-x); # function
dzdx:=diff(z,x); # derivative
solve(dzdx=0,x); # find where derivative is zero
plot(z,x=0..6); # plot and see that it agrees
z:='z'; # a command to stop and let us see the graph
1. A ball is thrown straight up. The ball's height as a function of time is given by y(t) = 6 + 50*t -16t^2.
a) Plot this function to see if it is reasonable.
b) Obtain the derivative of y with respect to time. Plot it and verify that it is positive while the ball is going up to the highest point and negative thereafter.
c) Before doing any calculations, predict the nature of the vertical acceleration as a function of the time. Then do the calculation and compare to your prediction.
2. The expression for the magnetic field on the axis of a circular loop, radius R, current I at a distance z from the center (along the axis) is
Bz:=km*Pi*i*R^2/(z^2+R^2)^(3/2);
a) Show there is a max at z=0 by plotting. Set R=1, I=1, km=1 and plot vs z.
Bz1:=subs(km=1,R=1,i=1,Bz);
plot(Bz1,z=-2..2);
b) Find the 2nd derivative, and locate where it is zero
Bz2:=diff(Bz,z,z); # 2nd derivative
solve(Bz2=0,z);
c) Create a Helmholtz coil (2 coils whose inflection points are the same, coil separation is twice the distance from center to inflection point.)
3. You are defending against incoming protons of KE 20,000 eV. You have a total charge of 500 nC to defend a 1-m wide region of space.
a) [For students] Show that the potential must be >20,000 V to do the job.
b) Show that a single charge in the middle won't do the job (use subs cmd).
Vcenter:=subs(q=500,a=1/2,y=0,z=0,V);
plot(Vcenter,x=0..1,y=0..30000);
c) Try half the charge at the 1/3 and 2/3 points (use subs command)
4. [loop and subs problem]
Find the potential at x=10 m from a group of 20 charges, each 5 nC, distributed uniformly from 0 to 1 m.
[Students, estimate the answer before doing calculations!] [Should look something like a point charge @ 1/2 m.]
End of easy2.mws