Mombu the Science Forum sponsored links

Go Back   Mombu the Science Forum > Science > independent variable problem in function definition
User Name
Password
REGISTER NOW! Mark Forums Read

sponsored links


Reply
 
1 3rd January 15:12
nma124
External User
 
Posts: 1
Default independent variable problem in function definition



Hello, This is mma 5.0, and maple 9.03

I am having some problem using a function in mma that I defined as
follows:

fd[z_]:= D[ f[z] , z ]

So the above is supposed to define the first derivative of f[z], which
in turn I have defined somewhere else as

f[z_]:= 1-Cos[z]

The problem shows up when I try to plot fd[z]. Then 'z' is passed
as a number to fd[z] during plotting, say 4, and then mma complains,


D[ f[4], 4 ] <--- the second argument here is NOT a symbol any more.

So, is there a way to resolve this?

This below are the 3 lines of code that show this problem,
and the similar maple code. Maple has no problem with this way to
doing it. (Any one knows how then maple handes this case? is the
function in maple not then called the same way as mma calls? why
does maple not give me the same error?)

----- mma code -----------------
f[z_] := 1 - Cos[z]
fd[z_] := D[ f[z] , z ]
Plot[fd[z], {z, -8, 8}]

General::ivar: -8. is not a valid variable.

--- maple code ----------

WORKS OK
  Reply With Quote


  sponsored links


2 3rd January 15:12
dan
External User
 
Posts: 1
Default independent variable problem in function definition



Hi. I think this is what you are looking for.
f[z_] := 1 - Cos[z]

fd[z_] := D[f[z], z]

The "key" is to be aware of the attributes of "Plot."

Attributes[Plot]
{HoldAll, Protected}

So "Plot" holds all of its arguments from evaluation. Therefore, you need
to "Evaluate" your function inside of Plot.

Plot[Evaluate[fd[z]], {z, -8, 8}];

Here's a small demo to show the difference:
x = 3;

Plot[x, {x, 0, 5}];

So the question is...how will this be plotted? Is x a constant '3', or is
it a blank variable that increases as the plot goes to 5?

By "Holding" the evaluation, you get the desired plot from (0,0) to (5,5)
However, in this example, we evaluate x to the value of '3' and we get a
horizontal line at y=3

Plot[Evaluate[x], {x, 0, 5}];
HTH. :>)
--
Dana DeLouis
Using Windows XP & Office XP
= = = = = = = = = = = = = = = = =
  Reply With Quote
3 3rd January 15:13
paul abbott
External User
 
Posts: 1
Default independent variable problem in function definition


Why are you using _delayed_ assignment here? Why not use Set:

f[z_] = 1-Cos[z]

You can also write

fd[z_] = D[ f[z] , z ]

to compute the derivative immediately (rather than _each time_ fd is
called). The difference between immediate and delayed assignment is
important, at least in Mathematica. For example, compare

r = Random[]
Table[r, {3}]

with

r := Random[]
Table[r, {3}]

This simple example indicates why both types of assignment are
implemented. Sometimes you might want to work with a fixed random number
and at other times you want a different random number upon each invocation.


Either use immediate assignment, as above, and then

Plot[fd[z], {z, -8, 8}];

works without problem or Evaluate the argument of Plot:

Plot[Evaluate[D[ f[z] , z ]], {z,-8,8}];

You can also do

Plot[Evaluate[f'[z]], {z,-8,8}];

which is a little more elegant.

Cheers,
Paul

--
Paul Abbott Phone: +61 8 9380 2734
School of Physics, M013 Fax: +61 8 9380 1014
The University of Western Australia (CRICOS Provider No 00126G)
35 Stirling Highway
Crawley WA 6009 mailtoaul@physics.uwa.edu.au
AUSTRALIA http://physics.uwa.edu.au/~paul
  Reply With Quote
4 5th January 22:37
nma124
External User
 
Posts: 1
Default independent variable problem in function definition


I am using := becuse I read in the book that whenever I define
a function I should use := and not =

Ok, thanks. But things are confusing enough for me without having
to worry when to := and when to use =

So I think I'll use := and call Evaluate when using Plot, so that
to be consistant with function definitions.

thanks for the info , very good to know this.
  Reply With Quote
5 5th January 22:37
paul abbott
External User
 
Posts: 1
Default independent variable problem in function definition


The relevent section of the book, 2.5.8 says:

One rule of thumb is the following. If you think of an assignment as
giving the final "value" of an expression, use the = operator. If
instead you think of the assignment as specifying a "command" for
finding the value, use the := operator. If in doubt, it is usually
better to use the := operator than the = one.

When there is no doubt, as in

f[z_] = 1-Cos[z]

you should use assignment. It is more efficient than using :=.

But the distinction _is_ important and both types of assignment are
required. See also 2.5.9 of the Mathematica book.

Not only Plot ...

Cheers,
Paul

--
Paul Abbott Phone: +61 8 9380 2734
School of Physics, M013 Fax: +61 8 9380 1014
The University of Western Australia (CRICOS Provider No 00126G)
35 Stirling Highway
Crawley WA 6009 mailtoaul@physics.uwa.edu.au
AUSTRALIA http://physics.uwa.edu.au/~paul
  Reply With Quote
Reply


Thread Tools
Display Modes




Copyright © 2006 SmartyDevil.com - Dies Mies Jeschet Boenedoesef Douvema Enitemaus -
666