Making a Function file which calls another function file

I have this gaussian2 m file:
function [g] = gaussian2(t, tau)
g = (1/sqrt(2*pi)) .*(1/tau) .* exp(- (t .^2) / (2 * tau^2));
end
I want to make another function m file of RMStau:
sqrt( integral( @(t) t.^2 .* gaussian2(t,tau), -inf, inf ) / integral( @(t) gaussian2(t,tau), -inf, inf ))
How do I make a RMStau m file as a function of my gaussian2 input?
Thanks

 採用された回答

Ced
Ced 2016 年 4 月 8 日
編集済み: Ced 2016 年 4 月 8 日

0 投票

I am not sure if this is want you are asking, but you can pass a function handle as input. A mini example would be
function y = eval_myfun(h_fun,input)
y = h_fun(input);
end
and then use it as
f =@(x)sin(x);
y = eval_myfun(f,2) % this will evaluate f at x == 2
In your case, you would first generate handle to a purely time-dependent function
h_gauss = @(t)gaussian2(t,tau);
and then pass h_gauss as handle to RMStau.
For RMS however, I don't see why you wouldn't simply do this in two steps.
1. Evaluate function y = gaussian2(t,tau);
2. compute RMS

5 件のコメント

petahadron
petahadron 2016 年 4 月 8 日
Thanks for your reply, but I am still confused.
I want to be able to run this RMStau(gaussian(t,5.5)); the answer would be equal to 5.5.
I am doing this assignment in which the RMS is defined as above (or picture below).
Walter Roberson
Walter Roberson 2016 年 4 月 8 日
Is there a connection between gaussian in your RMStau(gaussian(t,5.5)) and the gaussian2 function you are defining? Or is the idea that you would use that formula for gaussian to generate the tau value that is input to RMStau ? If that is the case then the code I gave should work.
Ced
Ced 2016 年 4 月 8 日
It depends what you want the input of your RMS function to be. If you want to be able to pass any function of t and compute the RMS, then you can use my method above, i.e.
function r = RMSfun(h_fun)
r = sqrt( integral( @(t) t.^2 .* h_fun(t), -inf, inf ) / integral( h_fun, -inf, inf ));
end
Then, for e.g. tau = 5.5, you would first generate the function handle and pass this to RMStau, i.e.
tau = 5.5;
h_gauss = @(t)gaussian2(t,tau);
r = RMSfun(h_gauss);
If you want an RMS function specifically for this gaussian function and have tau as the input variable, see Walter's answer below.
petahadron
petahadron 2016 年 4 月 8 日
Thank you!!!!
This is how I want it to work!
May I know why you did you put
@(t) and h_fun(t)
in
sqrt( integral( @(t) t.^2 .* h_fun(t), -inf, inf )
and not in
integral( h_fun, -inf, inf )
Ced
Ced 2016 年 4 月 9 日
Sure.
The integral function expects a function dependent on a single variable (t) as first argument. Now, for the first integral, you want to evaluate
t.^2.*h_fun(t)
but that is not a function handle, so I have to create a new one using
@(t)t.^2.*h_fun(t) % function handle to t.^2.*h_fun(t)
In the second case, want to evaluate h_fun(t), but h_fun is already a function handle, so there is no need to add another layer.
You could of course use
@(t)h_fun(t)
here too, but it's unnecessary.

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2016 年 4 月 8 日

0 投票

function r = RMStau(tau)
r = sqrt( integral( @(t) t.^2 .* gaussian2(t,tau), -inf, inf ) / integral( @(t) gaussian2(t,tau), -inf, inf ));

カテゴリ

ヘルプ センター および File ExchangeExponents and Logarithms についてさらに検索

質問済み:

2016 年 4 月 8 日

コメント済み:

Ced
2016 年 4 月 9 日

Community Treasure Hunt

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

Start Hunting!

Translated by