Matlab keeps being "busy" when computing the integral of a function

Hello guys,
I'm having a problem running a very simple code and I cannot figure out why.
The code is to find the definite integral of a function whose coefficients are variant. One limit of the integral is also variant.
I have quite a few computations to make but I started with the first two.
syms g z
[a,b] = meshgrid(1.2:.02:1.36,0.54:.02:0.6);
k = ((a./b)+((a./b).^2-4).^(1/2))./(2*a);
for i = 1:1
for j = 1:2
g =(a(i,j)*z)^2/(1+(a(i,j)*z)^2)-b(i,j)*z;
c(i,j) = int(g, z, 0, k(i,j));
end
end
For i=1, j=1, the code works fine but for i=1, j=2, it takes like forever for Matlab to compute. I have to use ctrl+c to stop it.
I don't see any singular behaviours of the function in the interval of integration.
I'm using Matlab R2014a. Maybe there are some stupid errors in the code but for the moment I cannot see it.
Could anyone find the problem?
Thank you!

 채택된 답변

madhan ravi
madhan ravi 2018년 12월 26일
편집: madhan ravi 2018년 12월 26일
Just convert symbolic integral (int()) to numerical integral (integral()) using matlabFunction() because the symbolic integrator is finding it difficult to solve.
syms z
[a,b] = meshgrid(1.2:.02:1.36,0.54:.02:0.6);
k = ((a./b)+((a./b).^2-4).^(1/2))./(2*a);
c=zeros(size(k)); % preallocate
for i = 1:size(k,1) % I believe you want to calculate the integral for entire k
for j = 1:size(k,2)
g =matlabFunction((a(i,j)*z)^2/(1+(a(i,j)*z)^2)-b(i,j)*z);
c(i,j) = integral(g, 0, k(i,j));
end
end
Gives:
>> c
c =
Columns 1 through 7
0.0098 0.0177 0.0255 0.0334 0.0413 0.0492 0.0571
-0.0064 0.0007 0.0079 0.0151 0.0225 0.0298 0.0371
-0.0196 -0.0135 -0.0071 -0.0005 0.0062 0.0129 0.0197
-0.0295 -0.0247 -0.0193 -0.0136 -0.0077 -0.0016 0.0047
Columns 8 through 9
0.0649 0.0727
0.0445 0.0518
0.0266 0.0334
0.0110 0.0173
>>

댓글 수: 3

Thank you very much! This helped!
OHHHH...thanks a lot , I had the same problem..I solved with your answer.......Regards from Barcelona-Spain

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

추가 답변 (0개)

제품

릴리스

R2014a

태그

질문:

2018년 12월 26일

댓글:

2019년 7월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by