简体   繁体   中英

Make MATLAB function to evaluate the value of symbolic function

I want to make MATLAB function which have input argument is a symbolic function (x) and the value of x (valuex). The output is the value of function. I try to make this code:

function value=f(funct,valuex)
syms x;
value=subs(funct,x,valuex);

But it gives an error.

>> f(x^2-x,1)
Undefined function or variable 'x'.

How to fix it?

In this code I define myFunc1 such as your function(f), the output of this function is result and first input is "fun" and second input is "x". I used this function (myFunc1(@(x) x^2, 2)) in the code for first input I used arrow function @(x) x^2 (this function square input) and for second input I pass number 2 when I run this code the output is 4. for your code just replace "myFunc1(@(x) x^2-x, 1)" by f(x^2-x,1)

clc
clear
close all

%final_result = myFunc1(@(x) x^2, 2)
final_result = myFunc1(@(x) x^2-x, 1)
function result = myFunc1(fun, x)
    result = fun(x)
end

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