Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

To resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

Computing an integral inside a function

Asked by Daniel on 30 Jun 2013

Hi guys!

I need to compute an integral inside a function. To compute an integral I use int function, first argument of which is the function to be integrated. I specify this function inside my big function

function [Put]=Putprice(S,K,r,q,T,a1,a2,b1,b2,c,beta)

    function y=b(s)
        y=b1*s+b2;
    end

alpha=r-q+b;

f=a^2*exp(-2*abs(beta)*int(alpha,s,0,u));

tau=int(f,u,0,T);

all the other parameters are specified. What's wrong?

Daniel.

0 Comments

Daniel

Products

No products are associated with this question.

2 Answers

Answer by Wayne King on 30 Jun 2013
Edited by Wayne King on 30 Jun 2013

It really helps when you show what error you are getting and how you are calling the function at the command line. Give people a simple example so that they can reproduce your error.

int() expects symbolic variables, are you using symbolic variables here?

If not, look at the help for integral()

value = integral(@(x) 1/sqrt(2*pi)*exp(-x.^2./2),0,3)

2 Comments

Daniel on 30 Jun 2013

Yes, you are right. I want to compute a definite integral, so the function integral() suits here best.

The problem is that when I define a function for this integral(), it contains another integral, which is symbolic...

b = @(s) r-q+b1*s+b2;

a = @(u) (a1*u+a2)^2;

tau=integral(a*exp(-2*abs(beta)*int(b,s,0,u)),0,T);

Daniel on 30 Jun 2013

syms s;

b = r-q+b1*s+b2;

a = @(u) (a1*u+a2)^2*exp(-2*abs(beta)*int(b,s,0,u));

tau=integral(a,0,T);

Still cannot compute tau :(

Wayne King
Answer by Shashank Prasanna on 30 Jun 2013

Integral is for numerical integration. What are you expecting the result in?

Once you compute the symbolic integral use subs to evaluate the result:

http://www.mathworks.com/help/symbolic/subs.html

Or use matlabFunction to convert the integrated function into a matlab function handle:

http://www.mathworks.com/help/symbolic/matlabfunction.html

Ofcourse I am assuming that you know what the other parameters are.

1 Comment

Daniel on 30 Jun 2013

I expect a number at the end. T=1 and I want to calculate tau. should I use smth like this?

a = @(u) (a1*u+a2)^2*exp(-2*abs(beta)*matlabFunction(int(b,s,0,u)));

I'm really stuck at this step

Shashank Prasanna

Contact us