简体   繁体   中英

How can I find the value of a function at some point in Matlab?

For example, if I define the following in Matlab,

f(x) = x^2 + 3x - 2

How do I find what value the function evaluates to when x = 3?

Create a file called fm that contains:

function y = f(x)
y = x^2 + 3x - 2

then in the command window, type f(3) and you should get back ans=16

See documentation on creating functions in MATLAB: http://www.mathworks.com/help/techdoc/ref/function.html

You can also use polyval(p,x) where p = [1 3 -2] in your example (the coeffients of your polynomial in descending order) and you will get the value of the polynomial at that point x (in your case, you would pass in 3).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM