Message Boards Message Boards

0
|
6319 Views
|
5 Replies
|
0 Total Likes
View groups...
Share
Share this post:

How does this pure function work in the body of a module ?

Posted 9 years ago

I am busy with the book of Paul Wellin “Prog with Math” . I have something I don’t understand. In Chap6 “procedural programming” he explains how it works and uses an example of finding a root. This is the basic x(i+1) =x(i) – f(x(i))/f’(xi))

Define f[x_] := x^2-2 Vind the root with

**My problem is the pure function 'fun' ** How does that work in the body ? Fun=Function[fvar, x^2 -2] ??? . This is not correct , but how then??? Fvar should that not be defined beforehand ??

POSTED BY: Chiel Geeraert
5 Replies
Posted 9 years ago

Hi Dave,

Thank you for your answer. Paul Wellin has example Notebooks where the given example runs flawlessly , but not with me. Next time I use the Mathematica code as given in the Guidelines.

Best regards Chiel

POSTED BY: Chiel Geeraert
Posted 9 years ago

Hi Chiel,

As I read the code, fun is defined within the module as Function[fvar,expr]. For this definition to be useful, fvar and expr need to be previously defined such that expr is the expression defining the functional relationship and fvar is a name for a variable which appears in the expression. For that reason, I would expect both values to be defined, probably in the arguments passed to the module. I see that expr is so defined, but fvar is not. This makes me suspect that var appearing as an argument for findRoot should read fvar.

It is possible your posted code was altered in the posting process. The best way to post Mathematica code is to use the method recommended here: Community Guidelines

Kind regards,

David

POSTED BY: David Keith
Posted 9 years ago

Thank you both for your answers. But I entered the function module in my first message but somehow that disappeared. I will enter it again

findRoot [expr,{ var,init}, eps ] :=

Module[{ xi=init, fun=Function[fvar, expr ] },

While[ Abs[fun[xi] ] > eps,

xi=N[ xi - fun[xi] / fun'[xi] ];

{var -> xi} ]x^

f[x_ ] : = x^2 -2

findroot [ x^2 - 2, {x,2},0.0001] out {x -> 1.4..}

I do not completely understand the pure function 'fun' From what I learned in previous chapters I translate this (with above example) fun=Function[fvar, x^2-2] . But that is not the correct interpretation.

Why not fun=Function[x,x^2-2]. or something like fun=Function[fvar, fvar^2 -2] ? I really would like to understand how it works

A.Geeraert

POSTED BY: Chiel Geeraert
Posted 9 years ago

fun=Function[x,x^2-2] defines a function similar in behavior to fun

In your example above, fvar no doubt means "function variable" or some such. It should have been written as Fun=Function[fvar,fvar^2-2] or as Fun=Function[x,x^2-2]

POSTED BY: David Keith
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract