Tips for speeding up douple-loop integral - matlab

I currently have the following code:
function v3matrix1 = v3(l,eigendata,sphere)
l0=l;
v3matrix1=zeros(2*l0+1,2*l0+1);
for m0=-l0:1:l0
for m0p=-l0:1:l0
fun=#(theta,phi)conj(Ylm(l0,m0,theta,phi)).*(-gamma11(1,eigendata,sphere,theta,phi)).*partialTheta2(l0,m0p,theta,phi).*sin(theta);
v3matrix1(m0+l+1,m0p+l+1)=integral2(fun,0,pi,0,2*pi);
end
end
end
It takes ~21 mins to run. I have tried to follow similar advice to that given here (Speeding up a double loop over integrals with changing parameter function values in matlab) by getting rid of the for loops using "arrayfun" and "meshgrid" but that didn't speed it up. I was wondering if anyone has any other tips or suggestions that might speed this code up?
Thank you very much.

Related

matlab 3 equations result problems

I am not good at matlab but I have to write short programs for my exams, so I started to prepare.
3 equations So these must be solved with f solve and I have to display the result of these, but this time I have to start from the origo.
My code is:
x = fsolve(y,x)
function y = t(x)
y=zeros(3,1);
x=ones(3,1);
y(1)=x(1).^3-11*x(1)+x(2).^2+9;
y(2)=x(1)*x(2).^2+x(2)-10*x(1)-11*x(3);
y(3)=x(1)*x(2)+x(3).^4-10*x(2)+8;
end
So as I think my refering of the function is bad. I hope it is and no more mistake in it. The error is: https://imgur.com/a/Slbi1tV
Could you help me out?
Thanking you in advance and have a good health all of you!

Optimize nested for loops in Matlab using Vectorization

I am a newbie to Matlab and I am currently trying to optimize a nested for loop as below. The loop is currently running forever for my input.
for i = 1:size(mat,1)
for j = 1:size(mat,2)
mat(i,j) = some_mapping(mat(i,j)+1);
end
end
However I can't find a way to vectorize it. I have tried bsxfun and arrayfun but it does not seem to work (or even run more slowly than the loop).
Maybe I was doing it in a wrong way. Any help is appreciated!
As suggested by Andras Deak, if some_mapping is simply a look-up-table operation, then
mat = some_mapping( mat+1 );
Notes:
- In order of the mapping to work, the values of mat must be integers in the range [0..numel(some_mapping)-1].
- The size of some_mapping does not affect the size of the result, it will be identical in size to mat.

How to adapt my for loop to get it working with parfor of Matlab? (ODE solver)

Ive come a long way with programming as a newbie in Matlab, but now Im really stuck. Ive tried to 'slice' the variable dy, but unfortunately it didnt work. I guess the problem lies in the fact that i is dependent on SACn. Ive done this because the ODE solver needs a vector as input, not a matrix. Can someone please help me?
This is a snippet of code:
parfor SACn=0:SACnum-1
i=19+SACn*200:1:66+SACn*200;
dy(i-18+100,:) = alpha.*(1-y(i-18+100,:)).*(1./(1+exp(-((y(i,:) -th1)./k1))))-beta.*y(i-18+100,:); %#ok<*PFBNS,*PFPIE>
dy(i-18+150,:) = alpha.*(1-y(i-18+150,:)).*(1./(1+exp(-((y(i-18+100,:)-th2)./k2))))-beta.*y(i-18+150,:);
end
I tried something like this (didnt work though):
parfor SACn=0:SACnum-1
temp1dy=zeros(200,size(y,2));
temp2dy=zeros(200,size(y,2));
for i=19:1:66
temp1dy(i-18+100,:) = alpha.*(1-y(i-18+100,:)).*(1./(1+exp(-((y(i,:) -th1)./k1))))-beta.*y(i-18+100,:); %#ok<*PFBNS,*PFPIE>
temp2dy(i-18+150,:) = alpha.*(1-y(i-18+150,:)).*(1./(1+exp(-((y(i-18+100,:)-th2)./k2))))-beta.*y(i-18+150,:);
end
dy(SACn*200+1:(SACn+1)*200,:)=temp1dy;
dy(SACn*200+1:(SACn+1)*200,:)=temp2dy;
end
Additional info: I have vectorized my code in an ODE solver of Matlab, these are all the :) 's you see at the end of all variables such as y(i+18-100,:). Ive done this so that the ODE solver computes multiple values of dy for the same time point in parallel and then chooses the best option.
Please do not hesitate to ask for more details!

MATLAB help optimize code

Currently I am writing a MATLAB program, which generates a fractal, based off of user-defined inputs. The problem is that when I run this piece of code with large arrays it takes an enormous amount of time to process. I am looking for advice or suggestions on how to optimize this code. Any responses will be greatly appreciated.
The main program is
for j=1:1:rows
for k=1:1:rows
Z(j,k)=iter(Z(j,k),c1(j,k),niter,f1);%Z and c1 are complex number,niter is an integer f1 is a handle
end
end
The function iter is
function z=iter(z1,c,niter,f1)
for i=1:1:niter
z1=f1(z1,c);
if abs(z1)<=2
z=i;
else
z=i;
break;
end
end
end

matlab timer functions

I want to call function A in a program. This function has a for loop inside and I'd like to shorten amount of iterations but not just changing the upper limit. How could I do it in this case? I was thinking if Matlab is capable of doing something like: one timer inside a function (or maybe inside a loop) and second in the main program that calls this function? But only rough idea, I'm a beginner. Please feed back if this is good idea and how could it be implemented?
thank you!
It sounds like you're talking about having a maximum elapsed time condition in your loop, something along the lines of,
MAX_T = 10;
tic;
for n=1:NMAX
% Call your loop functions
.
.
% Break if youve spent too much time in the loop
if toc > MAX_T; break; end;
end
There are also ways of optimizing this, such as only checking the value of toc every N iterations.