フィルターのクリア

How to add two functions and display the sum?

19 ビュー (過去 30 日間)
Saumya
Saumya 2023 年 2 月 6 日
編集済み: Sarvesh Kale 2023 年 2 月 6 日
I am trying to add two functions together and display the addition as a new function.
f=@(x) x^2+x;
g=@(x) 3*x^2+5*x;
z=f(x)+g(x);
I want to print z as:
@(x) 4*x^2+6*x
How do I achieve this? The disp function only gives the output of z as @(x) f(x)+g(x)
  1 件のコメント
Dyuman Joshi
Dyuman Joshi 2023 年 2 月 6 日
編集済み: Dyuman Joshi 2023 年 2 月 6 日
function handles don't resolve the expression per say, for example -
a = 1.3;
b = .2;
c = 30;
parabola = @(x) a*x.^2 + b*x + c
parabola = function_handle with value:
@(x)a*x.^2+b*x+c
This is not displayed as
par = @(x) 1.3*x.^2 + 0.2*x + 30
par = function_handle with value:
@(x)1.3*x.^2+0.2*x+30
Your closest option I think is syms
syms f(x) g(x)
f(x)=x^2+x;
g(x)=3*x^2+5*x;
%symbolic function
z(x)=f(x)+g(x)
z(x) = 
%Converting the symbolic function into function handle
z=matlabFunction(z)
z = function_handle with value:
@(x)x.*6.0+x.^2.*4.0

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

回答 (1 件)

Sarvesh Kale
Sarvesh Kale 2023 年 2 月 6 日
編集済み: Sarvesh Kale 2023 年 2 月 6 日
Hi Saumya,
You can try the symbolic math to solve your issue
syms x
f = x*x + x ;
g = 3*x*x + 5*x ;
z = f + g
z = 
x = 3;
subs(f) % will give you f(3)
ans = 
12
subs(z) % will give you f(3) + g(3)
ans = 
54
You can refer the documentation of symbolic math library for symbolic functions

カテゴリ

Help Center および File ExchangeSymbolic Math Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by