How do I simplify the values of a function?

Hi! I want to calculate a function, lets say f(x), that for example, may have the definition f(x) = a*b + b*x, where "a" and "b" are known values in my workspace. I want MATLAB to simplify this function for me. For example, if a = 2 and b = 4, I would like to get f(x) = 8+4x. I know I can define this function as an anonymous function, but that doesn't simplify the function, it simply states it. How can I get the function in a simplified way?
Thanks in advance

 採用された回答

madhan ravi
madhan ravi 2020 年 10 月 19 日
編集済み: madhan ravi 2020 年 10 月 19 日

0 投票

>> a
a =
2
>> b
b =
4
>> syms x
f(x) = a*b + b*x
f(x) =
4*x + 8
>>

4 件のコメント

madhan ravi
madhan ravi 2020 年 10 月 19 日
I think you maybe looking for subs() to substitute the values.
doc subs
Guillem Campins
Guillem Campins 2020 年 10 月 19 日
That doesn't work for me, it complains that x is not defined.
madhan ravi
madhan ravi 2020 年 10 月 19 日
Check the edited code.
Guillem Campins
Guillem Campins 2020 年 10 月 19 日
Great! Thanks very much

サインインしてコメントする。

その他の回答 (1 件)

Stephan
Stephan 2020 年 10 月 19 日
編集済み: Stephan 2020 年 10 月 19 日

0 投票

Can be done with symbolic toolbox - matlabFunction returns an anonymous function the way you want:
syms x
% known constants
a = 2;
b = 4;
% equation as symbolic
eq = a*b + b*x;
% create function handle
fun = matlabFunction(eq)
result:
fun =
function_handle with value:
@(x)x.*4.0+8.0

2 件のコメント

Guillem Campins
Guillem Campins 2020 年 10 月 19 日
Thanks very much!
Stephan
Stephan 2020 年 10 月 19 日
編集済み: Stephan 2020 年 10 月 19 日
Did you notice that you can accept and/or vote for useful answers?

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeSymbolic Math Toolbox についてさらに検索

製品

質問済み:

2020 年 10 月 19 日

編集済み:

2020 年 10 月 19 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by