1
votes

I am trying to evaluate integrals of different functions. I hope the code works such that for any given function I input, it evaluates the integral. The integral is with respect to the variable tau and there is another parameter t in the integrand.

I have tried defining the integrand first, and then pass it to the integral function.

integrand = @(tau,t,f0) double(t./(1.+tau).*double(f0(tau))); %tau is the independent variable, t is a parameter, and f0 is the function to be integrated
f_int = @(tau,t) integrand(tau,t,sin) %substitute f0 with sin 
integrate = @(t) integral(@(tau) f_int(tau,t),xmin,xmax)  %this part does the integration depending on t 

integrate(1) %integrate with t=1

And it shows the following errors.

Error using sin Not enough input arguments.

Error in IDE Trial>@(tau,t)integrand(tau,t,sin)

Error in IDE Trial>@(tau)f_int(tau,t)

Error in integralCalc/iterateScalarValued (line 314) fx = FUN(t);

Error in integralCalc/vadapt (line 132) [q,errbnd] = iterateScalarValued(u,tinterval,pathlen);

Error in integralCalc (line 75) [q,errbnd] = vadapt(@AtoBInvTransform,interval);

Error in integral (line 88) Q = integralCalc(fun,a,b,opstruct);

Error in IDE Trial>@(t)integral(@(tau)f_int(tau,t),xmin,xmax)

Error in IDE Trial (line 29) integrate(1)

I realized that I cannot just put sin in the function without passing any arguments, and there is probably a much better way of doing this. Is there anyway to fix the code such that it can do the job? Any help will be great. Thanks!

1

1 Answers

1
votes

You can pass sin or any other function as a function handle using @:

integrand = @(tau,t,f0) double(t./(1.+tau).*double(f0(tau))); 
f_int = @(tau,t) integrand(tau,t,@sin) %substitute f0 with sin

Or create an anonymous function first, and then directly pass its handle:

fun = @(t) exp(t)*sin(t); % some combination of functions 

integrand = @(tau,t,f0) double(t./(1.+tau).*double(f0(tau))); 
f_int = @(tau,t) integrand(tau,t,fun) %substitute f0 with fun

Since fun is already a handle, you don't need to add the @.