Matlab 2022a on Linux hangs when plotting/saving figures

6 views (last 30 days)
As titled, I tried to plot using the following script:
syms n
fplot(@(x) pi/4+symsum((1-(-1)^n)/(n^2*pi)*cos(n*x)+1/n*sin(n*x),n,1,15),[-15 15],'b')
ylim([-2 4])
xlabel('x')
ylabel('f(x)')
legend('k=15', 'Location', 'southeast')
I'd like to know if it is that this script is comuptational-intensive (I'm pretty new to Matlab) or is there something in Matlab that I need to configure to make it run faster. What I've experienced is as follows, along with my platform information:
Ubuntu 20.04.4, Intel Core i7-8565U CPU @ 1.8HGz x 8, 8GB RAM
After clicking the Run button to run the scropt above, I saw in the Command Window that the command returned quickly (within about one second) but the resulting figure window popped out only after several minutes. In the meantime, I saw one of my CPU core raised to 100% utilization (I saw this using `top').
After the figure showed up, I tried to save it as a PDF file, and that action took more than 15 minutes before the PDF file finally showed up in my folder.
I run my Matlab from the command-line prompt, with the following script:
#!/bin/bash
export LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libstdc++.so.6
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/lib/x86_64-linux-gnu/dri/
export MESA_LOADER_DRIVER_OVERRIDE=i965
matlab -desktop
I saw error message "Failed to load module "canberra-gtk-module"" but from some other threads on the forum it seems that that error message causes no real harm.
Best regards,
Chao

Accepted Answer

Prateekshya
Prateekshya on 9 Nov 2023
Hi Chao,
As per my understanding you want to reduce the time for creating the plot. The script is indeed computationally intensive. You can replace your script with the following code that will generate the .pdf file as well as the figure.
figure;
f = @(x) pi/4;
for n = 1:15
f = @(x) f(x) + (1-(-1)^n)/(n^2*pi)*cos(n*x) + 1/n*sin(n*x);
end
x = linspace(-15, 15, 10000); % Generate points between -15 and 15
y = f(x); % Evaluate the function for the given x values
plot(x, y, 'b');
% code to generate pdf
set(gcf,'Units','inches');
screenposition = get(gcf,'Position');
set(gcf,...
'PaperPosition',[0 0 screenposition(3:4)],...
'PaperSize',[screenposition(3:4)]);
print -dpdf -painters fig
% code to generate pdf ends
xlabel('x');
ylabel('f(x)');
legend('k=15', 'Location', 'southeast');
ylim([-2 4])
The line where "linspace" function is used, you can change the value of "10000" to other numbers and check which one gives you the appropriate plot.
I hope this helps!

More Answers (0)

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!