Merging/Multiplying Functions

조회 수: 246 (최근 30일)
yuval . 2017년 4월 19일
댓글: Star Strider . 2022년 4월 24일
Hello, I haven't figured out to find a way to create a new function out of multiplying 2 functions, for example:
fun1 = @(x) sin(x);
fun2 = @(x) cos(x);
*I want to create fun3 out of them so that -*
fun3 = @(x) sin(x)*cos(x)
The reason I'm asking this is that if I define fun3 as-
fun3 = @(x) fun1(x)*fun2(x)
and obviously it doesn't turn into-
fun3=@(x) sin(x)*cos(x)
thus I can't integrate fun3-
integral(fun3,0,5) ~= int(sin(x)*cos(x),x,0,5)
Matlab just says-
Error using *
Inner matrix dimensions must agree.
Thank you

채택된 답변

Star Strider
Star Strider 2017년 4월 19일
You need to use element-wise operations here, using the dot (.) operator for element-wise multiplication, (.*).
This works:
fun1 = @(x) sin(x);
fun2 = @(x) cos(x);
fun3 = @(x) fun1(x).*fun2(x);
int_fun3 = integral(fun3, 0, 5)
int_fun3 =
See the documentation on Array vs. Matrix Operations for a full discussion.
  댓글 수: 4
Star Strider
Star Strider 2022년 4월 24일
Thank you!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)


Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by