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

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!

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!

How to improve the speed of this code with double loops in Matlab? Is it possible?

Dear community of stack overflow. Is it possible to improve the speed of this code in Matlab? Can I use vectorization? Note that I have to use in every loop the "vpasolve" or "fsolve" function.
Here is the code which calls the function with a double loop:
for i=1:1000
for j=1:1000
SilA(i,j)=SolW(i,j);
end
end
Here is the function which is called by the above code. Can I vectorize the input of w, xi and make the code run faster?
function [ffw] = SolW(w,xi)
format long e;
z=0;mm=0.46;sop=80;
epit=0.1;voP=80.;
rho=2.1;aposC=0.1;aposD=0.1;
parEh=0.2*10^6;parEv=0.2*10^6;parGv=0.074*10^6;
parpos=0.35;hp=0.2;Ep=30*10^6;
parposV=0.20;ll=0.15;dd=2*ll;
C11=(parEh*(parEv-parEh*parpos^2)/((1+parpos)*(parEv-parEv*parpos-2*parEh*parpos^2)))*(1+2*1i*aposD);
C13=(parEh*parEv*parpos/((parEv-parEv*parpos-2*parEh*parpos^2)))*(1+2*1i*aposD);
C33=((1-parpos)*parEv^2/(parEv-parEv*parpos-2*parEh*parpos^2))*(1+2*1i*aposD);
C44=parGv*(1+2*1i*aposD);
DD=(Ep*hp^3)/(12*(1-parposV^2));
a1=C44;
a2=C33;
c1=(C13+C44)*1i*xi;
c2=(C13+C44)*1i*xi;
b1=rho*w^2-C11*xi^2;
b2=rho*w^2-C44*xi^2;
syms xr
rsol=vpasolve((a1*xr+b1).*(a2*xr+b2)-c1*c2*xr==0,xr);
rsol=eval(rsol);
r=[-sqrt(rsol)];
r1=r(1,:);
r2=r(2,:);
Fdf1=sop*sqrt(2*pi/(1i*epit*xi))*exp(1i*(xi*voP+w)^2/(2*epit*xi));
BC11=C44*(r1-1i*xi*c2*r1/(a2*r1^2+b2));
BC21=(C13*1i*xi)-((C33*c2*r1^2)/(a2*r1^2+b2))+(DD*xi^4-mm*w^2+1i*aposC*w)*c2*r1/(a2*r1^2+b2);
BC12=C44*(r2-1i*xi*c2*r2/(a2*r2^2+b2));
BC22=(C13*1i*xi)-((C33*c2*r2^2)/(a2*r2^2+b2))+(DD*xi^4-mm*w^2+1i*aposC*w)*c2*r2/(a2*r2^2+b2);
syms As1 As2;
try
[Ass1,Ass2]=vpasolve(BC11*As1+BC12*As2==0,BC21*As1+BC22*As2+Fdf1==0,As1,As2);
A1=eval(Ass1);
A2=eval(Ass2);
catch
A1=0.0;
A2=0.0;
end
Bn1=-(c2*r1/(a2*r1^2+b2))*A1;
Bn2=-(c2*r2/(a2*r2^2+b2))*A2;
ffw=Bn1*exp(r1*z)+Bn2*exp(r2*z);
end
Everything in your function but the calls to vpasolve, and try.... can be vectorize.
First, all this does not depend on w or xi, so could be computed once only:
format long e;
z=0;mm=0.46;sop=80;
epit=0.1;voP=80.;
rho=2.1;aposC=0.1;aposD=0.1;
parEh=0.2*10^6;parEv=0.2*10^6;parGv=0.074*10^6;
parpos=0.35;hp=0.2;Ep=30*10^6;
parposV=0.20;ll=0.15;dd=2*ll;
C11=(parEh*(parEv-parEh*parpos^2)/((1+parpos)*(parEv-parEv*parpos-2*parEh*parpos^2)))*(1+2*1i*aposD);
C13=(parEh*parEv*parpos/((parEv-parEv*parpos-2*parEh*parpos^2)))*(1+2*1i*aposD);
C33=((1-parpos)*parEv^2/(parEv-parEv*parpos-2*parEh*parpos^2))*(1+2*1i*aposD);
C44=parGv*(1+2*1i*aposD);
DD=(Ep*hp^3)/(12*(1-parposV^2));
a1=C44;
a2=C33;
From what I know, eval is slow, and I'm pretty sure that you don't need it:
rsol=eval(rsol);
Here is an example of vectorization. You should first generate all indices combination using meshgrid, and then use the . to noticed matlab to use element wise operations:
[I, J] = meshgrid(1:1000, 1:1000)
c1=(C13+C44)*1i.*xi;
c2=(C13+C44)*1i.*xi;
b1=rho.*w.^2 - C11.*xi.^2;
b2=rho.*w.^2-C44.*xi.^2;
But you won't be able to vectorize vpasolve, and try.... litteraly, without changing it to something else. vpasolve is probably the bottleneck of you computation (verify this using matlab profiler), so optimizing as proposed above will probably not reduce your computation time much.
Then you have several solutions:
use parfor if you have access to it to parallelize your computations, which depending on your architecture, could give you a 4x speedup or so.
it may be possible to linearize your equations and solve them all in one operation. Anyway, using a linear solver will be probably much faster than using vpasolve. This will probably give you the fastest speedup (guessing factor 10 -100 ?)
because you have continuous data, you could reduce the number of steps, if you dare loosing precision.
Hope this helps
In the above program everything can be vectorized as #beesleep said above.
For example:
[I, J] = meshgrid(1:1000, 1:1000)
c1=(C13+C44)*1i.*xi;
c2=(C13+C44)*1i.*xi;
b1=rho.*w.^2 - C11.*xi.^2;
b2=rho.*w.^2-C44.*xi.^2;
The vpasolve part ,i.e.,
syms xr
rsol=vpasolve((a1*xr+b1).*(a2*xr+b2)-c1*c2*xr==0,xr);
rsol=eval(rsol);
r=[-sqrt(rsol)];
r1=r(1,:);
r2=r(2,:);
can also be vectorized by using fsolve as it is shown here:
fun=#(Xr) (a1.*Xr+b1).*(a2.*Xr+b2)-c1.*c2.*Xr
x01=-ones(2*Nxi);
x02=ones(2*Nxi);
options.Algorithm= 'trust-region-reflective'
options.JacobPattern=speye(4*Nxi^2);
options.PrecondBandWidth=0;
[rsol1]=fsolve(fun,x01,options);
[rsol2]=fsolve(fun,x02,options);
The other part, i.e,
syms As1 As2;
try
[Ass1,Ass2]=vpasolve(BC11*As1+BC12*As2==0,BC21*As1+BC22*As2+Fdf1==0,As1,As2);
A1=eval(Ass1);
A2=eval(Ass2);
catch
A1=0.0;
A2=0.0;
end
since contains linear equations and in each row has two dependent equations can be solved as it is shown here:
funAB1=#(As1) BC11.*As1+BC12.*((-Fdf2-BC21.*As1)./BC22);
x0AB=ones(2*Nxi)+1i*ones(2*Nxi);
options.Algorithm= 'trust-region-reflective';
options.JacobPattern=speye(4*Nxi^2);
options.PrecondBandWidth=0;
[A1]=fsolve(funAB1,x0AB,options);
A2=((-Fdf2-BC21.*A1)./BC22);
However, both can also be solved analytically, i.e.,
AAr=a1.*a2;
BBr=a1.*b2+b1.*a2-c1.*c2;
CCr=b1.*b2;
Xr1=(-BBr+sqrt(BBr^.2-4.*AAr.*CCr))./(2.*AAr);
Xr2=(-BBr-sqrt(BBr^.2-4.*AAr.*CCr))./(2.*AAr);
r1=-sqrt(Xr1(:,:));
r2=-sqrt(Xr2(:,:));

Tips for speeding up douple-loop integral

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.

Numeric integration in Matlab

I want to do a numerical integration of the following equation in Matlab.
For simplicity, lets pretend that only the second fraction of the equation is there.
And this is what I have tried so far:
for lambda=280:2500
numerator(lambda) = trapz(Einc(lambda:lambda+1)*St(lambda));
denominator(lambda) = trapz(Eref(lambda:lambda+1)*St(lambda));
end
Fraction= nansum(numerator)/ nansum(denominator);
St is not defined in the entire 280-2500 range, that is why I use nansum at the end. But overall I feel that what I’m doing is wrong and that there should be a much more normal and correct way of calculating this, yet I have tried googling this and asked on Mathworks forum as well but no one answers me even though this should be a pretty common thing to calculate… Could someone please help me how do write this formula?

Avoiding loops in MatLab code (barycentric weights)

After having learned basic programming in Java, I have found that the most difficult part of transitioning to MatLab for my current algorithm course, is to avoid loops. I know that there are plenty of smart ways to vectorize operations in MatLab, but my mind is so "stuck" in loop-thinking, that I am finding it hard to intuitively see how I may vectorize code. Once I am shown how it can be done, it makes sense to me, but I just don't see it that easily myself. Currently I have the following code for finding the barycentric weights used in Lagrangian interpolation:
function w = barycentric_weights(x);
% The function is used to find the weights of the
% barycentric formula based on a given grid as input.
n = length(x);
w = zeros(1,n);
% Calculating the weights
for i = 1:n
prod = 1;
for j = 1:n
if i ~= j
prod = prod*(x(i) - x(j));
end
end
w(i) = prod;
end
w = 1./w;
I am pretty sure there must be a smarter way to do this in MatLab, but I just can't think of it. If anyone has any tips I will be very grateful :). And the only way I'll ever learn all the vectorizing tricks in MatLab is to see how they are used in various scenarios such as above.
One has to be creative in matlab to avoid for loop:
[X,Y] =meshgrid(x,x)
Z = X - Y
w =1./prod(Z+eye(length(x)))
Kristian, there are a lot of ways to vectorize code. You've already gotten two. (And I agree with shakinfree: you should always consider 1) how long it takes to run in non-vectorized form (so you'll have an idea of how much time you might save by vectorizing); 2) how long it might take you to vectorize (so you'll have a better sense of whether or not it's worth your time; 3) how many times you will call it (again: is it worth doing); and 3) readability. As shakinfree suggests, you don't want to come back to your code a year from now and scratch your head about what you've implemented. At least make sure you've commented well.
But at a meta-level, when you decide that you need to improve runtime performance by vectorizing, first start with small (3x1 ?) array and make sure you understand exactly what's happening for each iteration. Then, spend some time reading this document, and following relevant links:
http://www.mathworks.com/help/releases/R2012b/symbolic/code-performance.html
It will help you determine when and how to vectorize.
Happy MATLABbing!
Brett
I can see the appeal of vectorization, but I often ask myself how much time it actually saves when I go back to the code a month later and have to decipher all that repmat gibberish. I think your current code is clean and clear and I wouldn't mess with it unless performance is really critical. But to answer your question here is my best effort:
function w = barycentric_weights_vectorized(x)
n = length(x);
w = 1./prod(eye(n) + repmat(x,n,1) - repmat(x',1,n),1);
end
Hope that helps!
And I am assuming x is a row vector here.