How can I do integration for this function of two variable
1 view (last 30 days)
Show older comments
Hi I am a new to matlab so please bear with me if something look so basics or incorrect presented. Your suggestions are of great help. here my question: I want to do integration for the fun=f(x) from x1 to x2. but that at each value of x, that x is increasing by xo which is f(fun) so then fun should be calculated at the new updated x, (x+xo). in another word I want to do integration of a function of two variables (y=f(x,y)) if I'm correct. the base code is as below (the function is Dk which need to be developed to the above mentioned purpose, the variable a_e, the increment at each a, is rp=f(dk)
function [q] = IntegrateF (a1,a2,C,m,U)
syms a;
deltaS= 100;R=5;
Lamda = 1/(1+a/R);
F1 = sqrt(sec((pi/2)*(R+a)/45))*sqrt(sec((pi/2)*(2*R/90)));
F2 = 1-0.15*Lamda + 3.46*Lamda^2 - 4.47*Lamda^3 + 3.52*Lamda^4;
F = F1*F2;
Dk= U*F* deltaS .* sqrt (pi*a_e); rp=(1/(2*pi)*(Dk/300)^2; a_e=a+rp
dNda = 1 ./ (C *(Dk).^m);fun = dNda;fun=matlabFunction(fun);
q = quad(fun,a1,a2);
0 Comments
Answers (2)
Alan Weiss
on 20 Aug 2018
I think that you have a few bad coding practices.
The sec function works on angles in radians, but you have factors of pi/2 and 45 or 90, which makes me believe that you would prefer to work in degrees. If this is true, then you might find the secd function more reliable, as it works in degrees directly, avoiding any mistakes you might make converting to and from degrees.
A more serious issue is the use of symbolic variables and matlabFunction. I see no reason to use symbolic variables for this function. The conversion to and from symbolic is quite time-consuming and possibly error-prone. I do not see you using any symbolic functionality such as computation of a gradient or integral. Therefore, I suggest that you code your integrand purely numerically.
function f = myfun(a,C,m,U)
deltaS= 100;R=5;
Lamda = 1./(1+a./R);
F1 = sqrt(sec((pi/2)*(R+a)/45))*sqrt(sec((pi/2)*(2*R/90)));
F2 = 1-0.15*Lamda + 3.46*Lamda.^2 - 4.47*Lamda.^3 + 3.52*Lamda.^4;
F = F1.*F2;
Dk= U.*F.* deltaS .* sqrt (pi*a_e); rp=(1/(2*pi)*(Dk/300).^2; a_e=a+rp
f = 1 ./ (C *(Dk).^m);
Use fun = @(a)myfun(a,C,m,U) as your integrand.
Now I am not sure that I completely understand what you are doing, as it seems to me that you have a function of one variable alone, namely a. So I might have something wrong in my translation of your code. But I hope that you understand what I have done, and how it keeps everything numeric rather than a combination of symbolic and numeric.
Good luck,
Alan Weiss
MATLAB mathematical toolbox documentation
1 Comment
Alan Weiss
on 20 Aug 2018
Oh, if you need to solve for Dk within the function, then call fzero in a loop. Replace the second-to-last line of the script with this:
rp0 = 0;
Dk = zeros(size(a));
for ii = 1:length(a)
f = @(rp)(1/(2*pi)*(U.*F.* deltaS .* sqrt (pi*(a(ii)+rp))/300).^2) - rp;
rp0 = fzero(f,rp0);
Dk(ii) = U.*F.* deltaS .* sqrt (pi*(a(ii)+rp0));
end
I hope that I got all of the parentheses correct...
Alan Weiss
MATLAB mathematical toolbox documentation
See Also
Categories
Find more on Symbolic Math Toolbox in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!