Defining a function in Mathematica

  • Context: Mathematica 
  • Thread starter Thread starter nikolafmf
  • Start date Start date
  • Tags Tags
    Function Mathematica
Click For Summary
SUMMARY

This discussion focuses on defining and evaluating functions in Mathematica, specifically when dealing with polynomials generated from a for loop. The user initially attempts to define a function using the syntax f[x_]:=p, but encounters issues when trying to evaluate it at a specific point. The solution provided involves using Function[k, p /. x -> k] to correctly evaluate the polynomial at a given value.

PREREQUISITES
  • Understanding of Mathematica syntax and function definitions
  • Familiarity with polynomial expressions in Mathematica
  • Knowledge of replacement rules in Mathematica (e.g., /.)
  • Basic programming concepts, particularly loops and variable assignments
NEXT STEPS
  • Explore advanced function definitions in Mathematica
  • Learn about the use of replacement rules in Mathematica
  • Investigate the evaluation of expressions in Mathematica
  • Study the creation and manipulation of polynomials in Mathematica
USEFUL FOR

Mathematica users, particularly those working with mathematical functions and polynomials, as well as educators and students in computational mathematics.

nikolafmf
Messages
112
Reaction score
0
Hello,

I know that in Mathematica one can define a function as follows:

f[x_]:=x^2.

The problem is, I get a polynomial from a for loop as an expression p=a*x^n+b*x^(n-1)+... Now I want to evaluate the polynomial at a specific point. I tried to define a function from the polynomial as follows:

f[x_]:=p

and to evaluate

f[2]

which gives me just the polynomial, not it's value at 2.

How can I evaluate this?


Nikola
 
Physics news on Phys.org
I have already found out:

f = Function[k, p /. x -> k]

:D
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K