MATLAB Code Not Plotting Solution and Stuck on "Busy"

Hello,
I'm working on a MATLAB script to plot a solution, but the program gets stuck, continuously showing "Busy" in the status bar, and no plot is generated. I've checked my code for errors but haven't found any obvious issues. What could be causing this behavior, and how can I resolve it?
Thank you for your help!
clear clc;
lambda1=0.4*1i;
lambda2 = 0; lambda3 =0; lambda4 = -1*1i; a =1; x =-0.001;
[A1, B2] = deal(1);
[A2, B1] = deal(0);
[A3, B4] = deal(1.2);
[A4, B3] = deal(1.5);
t = linspace(-10, 10, 800); % Adjust the range and number of points as needed
y = linspace(-10, 10, 800);
r1=zeros(length(x), length(y), length(t));
for k=1:length(x)
for l=1:length(t)
for m=1:length(y)
X1 = exp(-lambda1*a*y(m)*1i + t(l)*1i./(2*lambda1) + A1*(x(k) + y(m) + t(l)).^2);
X2 = exp(-lambda2*a*y(m)*1i + t(l)*1i./(2*lambda2) + A2*(x(k) + y(m) + t(l)).^2);
X3 = exp(-lambda3*a*y(m)*1i + t(l)*1i./(2*lambda3) + A3*(x(k) + y(m) + t(l)).^2);
X4 = exp(-lambda4*a*y(m)*1i + t(l)*1i./(2*lambda4) + A4*(x(k) + y(m) + t(l)).^2);
Y1 = exp(lambda1*a*y(m)*1i - t(l)*1i./(2*lambda1) + B1*(x(k) + y(m) + t(l)).^2);
Y2 = exp(lambda2*a*y(m)*1i - t(l)*1i./(2*lambda2) + B2*(x(k) + y(m) + t(l)).^2);
Y3 = exp(lambda3*a*y(m)*1i - t(l)*1i./(2*lambda3) + B3*(x(k) + y(m) + t(l)).^2);
Y4 = exp(lambda4*a*y(m)*1i - t(l)*1i./(2*lambda4) + B4*(x(k) + y(m) + t(l)).^2);
q2num = [X1 X2 X2 X4 0; Y1 Y2 Y3 Y4 0; X1./lambda1 X2./lambda2 X3./lambda3 X4./lambda4 1; Y1./lambda1 Y2./lambda2 Y3./lambda3 Y4./lambda4 0; X1./(lambda1.^2) X2./(lambda2.^2) X3./(lambda3.^2) X4./(lambda4.^2) 0];
den = [X1 X2 X2 X4; Y1 Y2 Y3 Y4; X1./lambda1 X2./lambda2 X3./lambda3 X4./lambda4; Y1./lambda1 Y2./lambda2 Y3./lambda3 Y4./lambda4];
r1(l,m,k)= ((det(q2num)./det(den)));
end
end
end
[dr1dx, dr1dy, dr1dt] = gradient(r1);
dr1dx = dr1dx/mean(diff(x));
dr1dy = dr1dy/mean(diff(y));
dr1dt = dr1dt/mean(diff(t));
p1=a + 1i*(dr1dy - dr1dx);
figure (1)
surf(y,t,abs(p1));
view(45,60);

1 Kommentar

It seems you're encountering performance issues due to the large number of iterations in your nested loops. This is because you have 800 points each for y and t, resulting in a very high computational load.
For initial testing, it's advisable to reduce the number of points to ensure the code executes correctly with a smaller dataset. For instance, you can use linspace(-10, 10, 100) instead of 800 for both y and t.
This reduction will allow the loops to complete more quickly and help you verify that the code functions as expected. Once you confirm that the code works with the smaller dimensions, you can gradually increase the number of points to balance between performance and accuracy.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Walter Roberson
Walter Roberson am 24 Jan. 2025

0 Stimmen

Your code runs, but takes a while, as you are doing 1 x 800 x 800 x 2 determinents.
Eventually though the surf() fails. y and t are each vectors of length 800, but p1 is 800 x 800 x 800 and it is not possible to surf() 3D data.

6 Kommentare

A
A am 25 Jan. 2025
Verschoben: Walter Roberson am 25 Jan. 2025
@Walter Roberson @prabhat kumar sharma I used linspace(-10, 10, 100). Now code run but encountered "Error using surf Data dimensions must agree."
r1=zeros(length(x), length(y), length(t));
so r1 is indexed first by x, then by y, then by t
for k=1:length(x)
for l=1:length(t)
for m=1:length(y)
well that's a little suspicious, as it is not in x y t order and neither is it in reverse of x y t order (t y x). But it might be okay
r1(l,m,k)= ((det(q2num)./det(den)));
r1 is being indexed in t y x order, which disagrees with the initialization of x y t order.
When length(x) is 1, you want r1 to be indexed in either t y x or y t x order, so that the final dimension being of length 1 will effectively make r1 into 2d instead of 3d. For efficiency, it is best to index first by the most rapidly changing array index (the innermost for loop.)
So.. it is best to initialize
r1 = zeros(length(t), length(y), length(x));
and to use
r1(m,l,k)= ((det(q2num)./det(den)));
and to use
surf(y, t, abs(p1(:,:,1)));
This code takes into account the fact that the first parameter to surf needs to be indices to the second dimension of the array (because the first parameter is the first independent variable and the first independent variable is drawn across the horizonal axes so it indexes the second dimension of the input array.)
A
A am 25 Jan. 2025
@Walter Roberson I did it as per your suggestions but now got "Error using gradient
Too many output arguments. Error in plots (line 56)
[dr1dy, dr1dt, dr1dx] = gradient(r1);"
Previously you were initializing r1 as 1 x 800 x 800 but the code was filling it in 800 x 800 x 1 of that (leaving the other 800 x 800 x 799 initialized to zero). So you used to be using 3D arrays.
With the re-arrangement r1 is being initialized and filled out as 800 x 800 x 1, which is "collapsing" to be the 2d array 800 x 800. That is why gradient(r1) is failing.
Now, one thing I forgot to do was to re-arrange the output order on the gradient call. But I also did not take into account that r1 would now be 2D.
So, the calls should now be something along the lines of
if ndims(r1) == 2
[GX, GY] = gradient(r1);
GZ = [];
else
[GX, GY, GZ] = gradient(r1);
end
where you will need to figure out what GX and GY and GZ mean in terms of the rearranged r1. And you will still have the problem that if ndims(r1) > 2 then surf() is going to fail for you.
@Walter Roberson still there is blank output.
clear clc;
delta1 = 2.5; delta2 = -0.1; delta3 = 1.2; delta4 =-4.6; a =0.8;
[A1, B2] = deal(0.4); [A2, B1] = deal(-0.4); [A3, B4] = deal(-0.2); [A4,B3] = deal(0.2);
lambda1 = delta1 + 1i*delta2; lambda2 = delta1 - 1i*delta2;
lambda3 = delta3 + 1i*delta4; lambda4 = delta3 - 1i*delta4;
x=0.001;
t = linspace(-10, 10, 100);
y = linspace(-10, 10, 100);
r1 = zeros(length(t), length(y), length(x));
for k=1:length(x)
for l=1:length(t)
for m=1:length(y)
X1 = exp(-lambda1*a*y(m)*1i + t(l)*1i./(2*lambda1) + A1*(x(k) + y(m) + t(l)).^2);
X2 = exp(-lambda2*a*y(m)*1i + t(l)*1i./(2*lambda2) + A2*(x(k) + y(m) + t(l)).^2);
X3 = exp(-lambda3*a*y(m)*1i + t(l)*1i./(2*lambda3) + A3*(x(k) + y(m) + t(l)).^2);
X4 = exp(-lambda4*a*y(m)*1i + t(l)*1i./(2*lambda4) + A4*(x(k) + y(m) + t(l)).^2);
Y1 = exp(lambda1*a*y(m)*1i - t(l)*1i./(2*lambda1) + B1*(x(k) + y(m) + t(l)).^2);
Y2 = exp(lambda2*a*y(m)*1i - t(l)*1i./(2*lambda2) + B2*(x(k) + y(m) + t(l)).^2);
Y3 = exp(lambda3*a*y(m)*1i - t(l)*1i./(2*lambda3) + B3*(x(k) + y(m) + t(l)).^2);
Y4 = exp(lambda4*a*y(m)*1i - t(l)*1i./(2*lambda4) + B4*(x(k) + y(m) + t(l)).^2);
q2num = [X1 X2 X2 X4 0; Y1 Y2 Y3 Y4 0; X1./lambda1 X2./lambda2 X3./lambda3 X4./lambda4 1; Y1./lambda1 Y2./lambda2 Y3./lambda3 Y4./lambda4 0; X1./(lambda1.^2) X2./(lambda2.^2) X3./(lambda3.^2) X4./(lambda4.^2) 0];
den = [X1 X2 X2 X4; Y1 Y2 Y3 Y4; X1./lambda1 X2./lambda2 X3./lambda3 X4./lambda4; Y1./lambda1 Y2./lambda2 Y3./lambda3 Y4./lambda4];
r1(m,l,k)= ((det(q2num)./det(den)));
end
end
end
if ndims(r1)
[Gx, Gy] = gradient(r1);
Gt = [];
else
[Gx, Gy, Gt] = gradient(r1);
end
% Normalizing each derivative by the spacing of the respective variable
Gx = Gx/mean(diff(x)); % x is a vector of x-coordinates
Gy = Gy/mean(diff(y)); % y is a vector of y-coordinates
Gt = Gt/mean(diff(t)); % t is a vector of t-coordinates
p1=a + 1i*(Gy - Gx);
figure (1)
surf(y, t, abs(p1(:,:,1)));
[Gx, Gy] = gradient(r1);
That is incorrect. The re-written r1 is in the order y, t, x.
You only have a single x. The gradient with respect to x is undefined, so when you do
p1=a + 1i*(Gy - Gx);
you have an operation between a well-defined gradient, and NaN.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Axes Appearance finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by