Saving multiple plots into one handle

8 visualizaciones (últimos 30 días)
Sobhan
Sobhan el 7 de Mzo. de 2024
Comentada: Voss el 8 de Mzo. de 2024
I am running a FEM program and need to plot the results for a few different solution iterations and the true solution. My FEM code runs in a loop so every loop I am plotting one solution but I noticed that drawing the figure in a loop is relatively slow compared to saving them all to a handle and plotting at the end. I am new to this method and saw you can use copyobj(), but it's not working correctly and I don't know what I am doing wrong here. Specifically my labels are not working correctly, so I suspect it is drawing too many lines. This is how it is supposed to look:
This is how it looks when I try to optimize the plotting time:
I have attached the file with the working (slow) plotting. What I changed to get the second result is in this (labelled %NEW):
color={'#0048BA','#C46210','#9F2B68','#3B7A57','#665D1E','#89CFF0','#FFB200','#7BB661','#00CED1','#DEB887'};
extraInputsLabel = {'interpreter','latex','fontsize',14};
extraInputsTitle = {'interpreter','latex','fontsize',24};
extraInputsAxis = {'interpreter','latex','fontsize',20};
ind1 = 0;
for m=1:p
ind1 = ind1+1;
ind2 = 1
while tol>tolerance
ind2 = ind2+1;
plt(ind2) = plot(x,uh,'Color',color{ind2},'DisplayName',strcat('$N_e = ',num2str(ne),'$'),LineStyle='--'); %NEW
hold on
end
plt(ind2+1) = plot(x,ut(x),'r','DisplayName','True sol'); %NEW
copyobj(plt,gca) %NEW
axis([0 L -2 2])
title(strcat('$p = ',num2str(m),'$'),extraInputsTitle{:})
xlabel('$x$',extraInputsAxis{:})
ylabel('$u(x)$',extraInputsAxis{:})
legend(extraInputsLabel{:})
figure(ind1+p)
plot(midx,err)
title('Element error',extraInputsTitle{:})
xlabel('$x$',extraInputsAxis{:})
ylabel('$\epsilon_e$',extraInputsAxis{:})
figure(ind1+2*p)
loglog(conv(:,2),conv(:,1))
title(strcat('Convergance plot $(n=',num2str(round(nc,2)),')$'),extraInputsTitle{:})
xlabel('$h_e$',extraInputsAxis{:})
ylabel('$\epsilon_e$',extraInputsAxis{:})
end
  2 comentarios
Alexander
Alexander el 7 de Mzo. de 2024
What is "p"?
Sobhan
Sobhan el 7 de Mzo. de 2024
Editada: Sobhan el 7 de Mzo. de 2024
It's just the degree of the elements, here p=3 so I am solving for m=1,2,3. But it's not important to the issue, as I experience this for every iteration of m.

Iniciar sesión para comentar.

Respuesta aceptada

Constantino Carlos Reyes-Aldasoro
Constantino Carlos Reyes-Aldasoro el 7 de Mzo. de 2024
Why don't you save your results in a variable, as a matrix x2(ind2,:) = x; uh2(ind2,:)=uh; and then once you have all, then you plot.
  9 comentarios
Sobhan
Sobhan el 8 de Mzo. de 2024
Alright, thanks! That worked and doubled the speed.
Voss
Voss el 8 de Mzo. de 2024
You're welcome!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Formatting and Annotation en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by