Calculations using Inline Functions

Hello... I'm still a beginner when it comes to using MATLAB and I can't seem to figure this equation out.
My Code:
t = (-100:0.01:100);
g = inline ('(exp(-t).*(cos(2*pi*t))).*(t>=0)','t');
h = inline ('g((2*t)+1) + g(1-t)','t','g');
E = inline('((g(2*t+1) + g(1-t)).^2)','t','g'); % square of the function h(t)
En = quad(E(h,t),-100,100);
En (E,t)
Error:
Error using inlineeval (line 14)
Error in inline expression ==> ((g(2*t+1) + g(1-t)).^2)
Undefined function 'mtimes' for input arguments of type
'inline'.
Error in inline/subsref (line 23)
INLINE_OUT_ = inlineeval(INLINE_INPUTS_,
INLINE_OBJ_.inputExpr, INLINE_OBJ_.expr);
Error in BA2 (line 27)
En = quad(E(h,t),-100,100);
Please and thank you.

 採用された回答

Adam
Adam 2015 年 2 月 13 日
編集済み: Adam 2015 年 2 月 13 日

1 投票

I've never used 'inline' and didn't even know it existed, but the documentation says:
'inline will be removed in a future release. Use Anonymous Functions instead.'
so I would suggest not using inline if you intend to keep your code for future usage too. I use anonymous functions a lot which is probably why I never knew about the 'inline' function.
It looks like you have too many closing parenthesis in your definition of 'g' though.
Try using the following instead as an anonymous function:
g = @(t) exp(-t) .* cos( 2 * pi * t ) .* ( t >= 0 );
h = @(t) g(( 2 * t ) + 1 ) + g( 1 - t )
E = @(t) h(t).^2
I think that should work for E...no need to define it in terms of g if it is just h squared.
Then I assume this should give the right answer:
En = quad( E, -100, 100 );
Again though the 'quad' function says it will be removed in a future release, but maybe if you are using an old Matlab you don't have the suggested replacement function.

3 件のコメント

Annalise
Annalise 2015 年 2 月 13 日
編集済み: Annalise 2015 年 2 月 13 日
That reduced the number of errors I've got, however, a new error shows up for E.
E = @(t) h(t).^2;
En = quad(E(h,t),-100,100);
En (h,t)
The error comes up as:
Error using @(t)h(t).^2
Too many input arguments.
Error in BA2 (line 27)
En = integral(E(h,t),-100,100);
Both with quad and integral, I keep getting the same error over and over.
Annalise
Annalise 2015 年 2 月 13 日
Oh.. I got it. Thank you very very much!!
Adam
Adam 2015 年 2 月 13 日
I'm not too familar with quad or integral in terms of how you feed in the single argument to the 'E' function, but:
En = quad( E, -100, 100 );
certainly gave a result for me without error. Whether it was a correct result or not I'm not sure.
Usually you would just call E as:
res = E(t);
but when it is being passed as a function handle to 'quad' or 'integral' that function appears to expect format more like the above.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeFunction Creation についてさらに検索

質問済み:

2015 年 2 月 13 日

コメント済み:

2015 年 2 月 13 日

Community Treasure Hunt

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

Start Hunting!

Translated by