Gabriel
Gabriel

Reputation: 13

How to input a function into GNUoctave?

I need to input a function through console into a second one. I have this function

trapez(fun,a,b,n)

so if I type this in the command line

trapez(@(x) x.^3,0,1,2) 

it works perfectly but I need to run a program that uses a string input from the user like: 'x^4'and it gets used in the trapez function. I need the 'x^4' to become a function usable for trapez. I have tried this:

t=input('func')
trapez(t,a,b,n)

and

t=input('func')
str2func(t)

but I get this error: error creating function handle "@x.^2"

Upvotes: 1

Views: 96

Answers (1)

juliohm
juliohm

Reputation: 3779

Use the inline function:

f = inline("x^2");
f(2)
=> 4

Upvotes: 1

Related Questions