Matlab taking so much execution time

I have a simple code here. But whenever I try to run this code, the system gets slower and start to hang frequently. Moreover, the run process seems to be never ending. Hence I forcefully shut down the pc several time. Pl , somebody see my code and solve the problem. Actually here, I am trying to get a 3d plot.
clc;clear;
syms theta phi b k alpha
alpha=1;
b=1;
sigma1=[0 1;1 0];
sigma2=[0 -1i;1i 0];
sigma3=[1 0;0 -1];
sigmap=1/2*(sigma1+1i*sigma2);
sigmam=1/2*(sigma1-1i*sigma2);
I=eye(2);
n=[sin(theta)*cos(phi) sin(theta)*sin(phi) cos(theta)];
a=sigma1*sin(theta)*cos(phi)+sigma2*sin(theta)*sin(phi)+sigma3*cos(theta);
d=kron(sigmap,sigmap)+kron(sigmam,sigmam);
h=1/2*alpha*b*kron(a,I)+k*d; %% a 4*4 matrix
[V,L]=eig(h);
u=V(:,1)./sqrt(sum(V(:,1).^2)); %%To make the normalization to the one of the eigen vector of h.
w=diff(u,phi); %% Derrivative of that eigen vector with respect to phi variable.
r=dot(u,w);
assume(theta>=0);
assume(phi>=0);
r=simplify(r,'Steps',100);
f=1/pi*1i*int(r,phi,0,2*pi);
f=simplify(f,'Steps',100);
ffcn=matlabFunction(f);
theta = linspace(0.001,4, 30);
k = linspace(0.001,10, 30);
[Th,K] = meshgrid(theta, k);
F=ffcn(Th,K);
figure
mesh(Th,K, F)
colormap(cool)
grid on
xlabel('\bf\theta','FontSize',14)
ylabel('\bf\alpha','FontSize',14)
zlabel('\bf\itf','FontSize',14)

7 commentaires

Walter Roberson
Walter Roberson le 27 Jan 2020
Modifié(e) : Walter Roberson le 27 Jan 2020
Simplification of a long expression is an expensive step, especially when you ask it to try hard with steps, 100. It can lead to a lot of memory use that can lead to swapping.
AVM
AVM le 27 Jan 2020
Thanks for your reply. But how can I do that alternatively?
AVM
AVM le 27 Jan 2020
Should I not to use 'simplify()' in this case? Without that does the plotting appear? Pl advise me.
Walter Roberson
Walter Roberson le 28 Jan 2020
You could experiment with instead using the 'File' option of matlabFunction, which defaults to running an optimization pass. It is not at all the same as simplification: it is common subexpress elimination. So it would not find cases where a sin^2+cos^2 could be simplified, but it would, for example, notice that it was not necessary to continually recalculate sin^2.
Unfortunately this optimization can be pretty slow. I suspect that it uses time proportional to the square of the number of subexpressions, so long expressions can take a long time to work through. The advantage is that when you finish the calculation is considerably faster.
But you still need to think about the tradeoffs. Assume that with an expression as long as yours that the optimization might take 12 hours (it easily could.) If drawing the graph without using optimization would take less than 12 hours and you only have to draw the graph once, then doing the optimization would not be worthwhile. If though there were reasons that you needed to plot multiple times from the same formula then the time savings from having done optimization could add up.
But seriously, if you use the File option for matlabFunction with that expression, then leave it overnight at least, and do not count on it being done by morning.
AVM
AVM le 28 Jan 2020
Thanks..okay,I am leaving it without any optimization (simplify() kind of thing) whole night.let see what happen..
@walter: I was trying without any optimization in the following code according to your advice, but without optimisation is taking more than 1day to excute but the execution yet not completeed.. It's really painful for me. Pl help me.
clc;clear;
syms theta phi b k alpha
alpha=1;
b=1;
sigma1=[0 1;1 0];
sigma2=[0 -1i;1i 0];
sigma3=[1 0;0 -1];
sigmap=1/2*(sigma1+1i*sigma2);
sigmam=1/2*(sigma1-1i*sigma2);
I=eye(2);
n=[sin(theta)*cos(phi) sin(theta)*sin(phi) cos(theta)];
a=sigma1*sin(theta)*cos(phi)+sigma2*sin(theta)*sin(phi)+sigma3*cos(theta);
d=kron(sigmap,sigmap)+kron(sigmam,sigmam);
h=1/2*alpha*b*kron(a,I)+k*d; %% a 4*4 matrix
[V,L]=eig(h);
u=V(:,1)./sqrt(sum(V(:,1).^2)); %%To make the normalization to the one of the eigen vector of h.
w=diff(u,phi); %% Derrivative of that eigen vector with respect to phi variable.
r=dot(u,w);
f=1/pi*1i*int(r,phi,0,2*pi);
theta = linspace(0.001,4, 30);
k = linspace(0.001,10, 30);
[Th,K] = meshgrid(theta, k);
F=f(Th,K);
figure
mesh(Th,K, F)
colormap(cool)
grid on
xlabel('\bf\theta','FontSize',14)
ylabel('\bf\alpha','FontSize',14)
zlabel('\bf\itf','FontSize',14)
Walter Roberson
Walter Roberson le 30 Jan 2020
Shrug. Get yourself a much much faster computer. Something overclocked and cooled with liquid nitrogen perhaps.

Réponses (1)

AVM
AVM le 28 Jan 2020

0 votes

Thanks..okay,I am leaving it without any optimization (simplify() kind of thing) whole night.let see what happen...

Cette question est clôturée.

Question posée :

AVM
le 27 Jan 2020

Clôturé :

le 20 Août 2021

Community Treasure Hunt

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

Start Hunting!

Translated by