Making a Function file which calls another function file
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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
0 commentaires
Réponse acceptée
Ced
le 8 Avr 2016
Modifié(e) : Ced
le 8 Avr 2016
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 commentaires
Ced
le 9 Avr 2016
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.
Plus de réponses (1)
Walter Roberson
le 8 Avr 2016
function r = RMStau(tau)
r = sqrt( integral( @(t) t.^2 .* gaussian2(t,tau), -inf, inf ) / integral( @(t) gaussian2(t,tau), -inf, inf ));
0 commentaires
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!