How can I do integration for this function of two variable

조회 수: 2 (최근 30일)
Wazy sky
Wazy sky 2018년 8월 20일
댓글: Alan Weiss 2018년 8월 20일
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);

답변 (2개)

Alan Weiss
Alan Weiss 2018년 8월 20일
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
Alan Weiss
Alan Weiss 2018년 8월 20일
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

댓글을 달려면 로그인하십시오.


Wazy sky
Wazy sky 2018년 8월 20일
편집: Wazy sky 2018년 8월 20일
Dear Alan Weiss
Thank you so much for your time, the valuable comments & corrections for the suggested code.
  • -for the sec function, its radians, and the number 45,90 have dimension units. Thanks for suggesting secd as I haven't used but it will be helpful to me for such purpose later.
  • -Yes I got many errors related to using the symbolic variable, and maybe from the matlabfunction, but could not figured out the sours. thanks for highlighting this point.
  • -My function Dk has variable a_e which is function of the function itself (a_e=a+(1/2pi)*( Dk*/300)^2). a_e is unidentified before solving dk, this led to error. I feel I need to fix this first.
  • my integration from a=a1 to a=a2. So if input only a, It does not seem I can do the definite integral.
  • Honestly, I am not totally sure how to use fun as my integral. for example in quad(fun,a1,a2) lead to error here.
Suggestions are so much appreciated

카테고리

Help CenterFile Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by