Jason Cawley
Wolfram Science Group
Phoenix, AZ USA
Registered: Aug 2003
Posts: 712 |
First, this is the NKS Forum, for discussion of Stephen Wolfram's book "A New Kind of Science". General Mathematica questions should go to our MathGroup discussion list instead. You can find that here -
http://forums.wolfram.com/mathgroup/
You can look for an existing answer in the archives or email a question. You can read it as a newsgroup, etc.
Since your question is simple, though, I can readily explain how you do it. The point of the exercise is presumably to learn something about derivatives yourself, however. Mathematica already knows all about them.
Although you don't really need to since it is so simple, you can define your function f as follows -
f[x_]:= Sin[3 x]
That creates a function called f, and accepts one argument for it, labeling whatever argument it gets "x". The underscore (read as "blank") is a pattern that matches any one thing. The := is a "SetDelayed", just like the single equals "Set", but not executed immediately. When f is given an argument, say f[2] and evaluated, then the delay ends and the expression is evaluated. The right hand side works like any Set. x is whatever argument f was passed. It is multipled by 3, and then the Sin is taken.
You can now treat f like any built in function, say to plot it -
Plot[f[x],{x,-4,4}]
Looks like any sine, just with 3 wavelengths within the usual period - that is all the 3 does.
Next you are asked to consider a new function, pf. OK, so write that one out the same way. The only wrinkle is now you have a new function for each value of h. Or you can think of it as a function with two arguments, x and h.
pf[x_, h_]:= (f[x+h] - f[x]) / h
Then you want pf of x with h set to each of {-.1, -.05, .05, .1}. You could do that manually by asking for pf[x,-.1] and then pf[x,-.05], or you can use a trick to get all of them at once. You let the h become a Slot (# symbol) and then use Map -
Plot[pf[x,#],{x,-4,4}]&/@{.-1,-.05,.05,.1}
That says, a new unnamed function (the & is shorthand for that) that puts its argument in the # position ("Slot") and Plots the results with x ranging from -4 to 4, then Map that function (/@ is shorthand for that) over the list {-.1,.-05,.05,.1}. The result when evaluated is 4 plots. Then you can compare any of them to -
Plot[3 Cos[3x], {x, -4, 4}]
And the point is, all of them are identical.
Notice the amplitude of all of them is from -3 to +3, while the original function had an amplitude of only -1 to 1. The maximum rate of change has to be higher when there are more wavelengths packed into the same period. That is the "3" coefficient coming outside when you take the derivative.
I hope this helps.
Report this post to a moderator | IP: Logged
|