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

Alexander
Alexander 2024년 3월 7일
What is "p"?
Sobhan
Sobhan 2024년 3월 7일
편집: Sobhan 2024년 3월 7일
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.

댓글을 달려면 로그인하십시오.

 채택된 답변

0 개 추천

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

Sobhan
Sobhan 2024년 3월 7일
How could I then specify the color, linetype and name for each plot? I am specifically thinking about the "strcat('$N_e = ',num2str(ne),'$')". I can't just make ne a vector with all the values, so would I need to type it out ind2 amount of times?
Voss
Voss 2024년 3월 7일
"I can't just make ne a vector with all the values"
Why not?
Sobhan
Sobhan 2024년 3월 7일
편집: Sobhan 2024년 3월 7일
I thought it wouldn't be able to concatenate if ne was nx1, but apparently it does and makes a character array with all the values in the right place. Thanks for the clarification!
Sobhan
Sobhan 2024년 3월 7일
I have a follow up question though, how well does cell storage work in matlab? As uh is a different length each iteration and I don't know the final size of the final uh, is storing them in a cell array efficient or is there a better alternative?
Voss
Voss 2024년 3월 7일
Using a cell array is a good method for storing multiple different-length vectors.
Sobhan
Sobhan 2024년 3월 7일
@Voss How would I plot it then without going through a for loop again? Given two cell arrays uh and x, where each cell element needs a label and a color from other arrays.
Voss
Voss 2024년 3월 7일
편집: Voss 2024년 3월 7일
Here's how you can plot from a cell array without using a loop:
x = {[1 2 3],[1 2 3 4],[1 2 3 4 5]};
uh = {[4 6 5],[8 9 10 7],[10 12 11 13 14]};
ne = [1 10 50];
color = {'#0048BA','#C46210','#9F2B68'};
names = compose('$N_e = %d$',ne);
plot_args = [x; uh];
h = plot(plot_args{:},LineStyle='--');
set(h,{'DisplayName'},names(:),{'Color'},color(:))
legend(h,'Interpreter','latex')
Sobhan
Sobhan 2024년 3월 8일
Alright, thanks! That worked and doubled the speed.
Voss
Voss 2024년 3월 8일
You're welcome!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Programming에 대해 자세히 알아보기

제품

릴리스

R2023a

태그

Community Treasure Hunt

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

Start Hunting!

Translated by