how to evaluate a mathematical function in matlab? -


i novice in matlab , searching, how solve mathematical function in matlab.

it's want, want solve function f(x)=x^2+2x+1, x=2. want take x^2+2x+1 , 2 input , show output.

from link, saw how solve mathematical function. procedure available in link is:

>>f = @(x) 2*x^2-3*x+4; >>f(3) ans = 13 

so, wrote following part in script:

f=input('enter function: ','s'); v = input('parameter: '); f=@(x)f; disp(f(v)); 

when provide x+1 , 3 input, ans getting x+1. how solve problem?

thanks in advance.

your f variable string says "x + 1". you'll have ask matlab interpret string function in order numerical value.

one way of doing using eval

f = input('enter function: ', 's' ); v = input('parameter: '); myfun = sprintf('@( %s ) %s', v, f ); % string f = eval( myfun ); % interpret string command f( 3 ), % math - evaulate f(3) 

edit, clarification based on comments below:
above solution assumes f, input string representing mathematical formula may have user-chosen variable (i.e., unknown not have 'x', may 'y', 'a' etc.) in order comunicate this, v char storing information.

in case f depends on 'x', , desired output f(v) numeric value v following modification needed:

myfun = sprintf('@(x) %s', f ); f = eval(myfun); f_of_v = f( v ) 

Comments

Popular posts from this blog

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

java - JavaFX 2 slider labelFormatter not being used -