How to use polynomial as an input for an integral function?
4 views (last 30 days)
Show older comments
I am building a code where I need to find a polynomial using values given from an interation, and then use the polynomial in a Gauss-Kronrod integral. In order to calculate this integral I need to build a function that gets the polynomial as an input (along with many other arguments) and uses that polynomial in the Gauss-Kronrod function. My problem is that Matlab does not accept the Poly2sym function as an input for the integral. I really need to use this function because I get the polynomial values from the Polyfit function and then I use the Poly2sym to transform that vector into a polynomial. My program is large so I have made a shorter sample of my problem:
function o = func(Bt,r,q,sigma,t)
o = Bt + ola(Bt,poly2sym([1 3 2]),r,q,sigma,t);
end
function F = ola(Bt,Bx,r,q,sigma,t)
F = quadgk(integral1(Bt,Bx,r,q,sigma,t),0,t);
end
function i1 = integral1(Bt,Bx,r,q,sigma,t)
i1 = @(x)(t-x).*d1(Bt,t-x,Bx,r,q,sigma);
end
function u = d1(S,t,B,r,q,sigma)
u =log(S./B)+(r-q+0.5*(sigma^2))*t;
end
My main question is: How do I use the poly2sym function here without an error, in a way that the function calculates the integral using poly2sym polynomial correctly?
This short sample is part of a much larger problem that I would be happy to provide to you if you can't understand my problem.
Thank you very much for your time.
0 Comments
Answers (2)
Walter Roberson
on 15 Oct 2013
integral1() is for numeric integration. When you want to do symbolic integration (poly2sym generates symbolic) then use the symbolic routine int().
You would probably want to use matlabFunction() on the result of int() if you are going to put it into a quadgk call. Or you could just int() it again symbolically and double() the rssult. Or you could use the symbolic toolbox numeric integration routines.
0 Comments
Filipe Pereira
on 15 Oct 2013
2 Comments
Walter Roberson
on 15 Oct 2013
When you get down to the d1 level, where you divide by the B which has been passed down from the poly2sym() result, then what is your expectation of what should happen there? Are you wanting the resulting "u" to be a function handle used by one of the layers above and upwards into the quadgk()? Or are you wanting to evaluate the polynomial at x at the place it shows B ?
See Also
Categories
Find more on Polynomials in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!