Error with the animation of the surface plot
조회 수: 3 (최근 30일)
이전 댓글 표시
Hello
I want to perform a surface animation, but I constantly run into an error
Error using surf
Data dimensions must agree.
Error in surfc (line 54)
hs = surf(cax, args{:});
Error in dcSQUID_diode_efficiency_animation (line 26)
surfc(phi,theta,eff)
I would be grateful if someone could tell me where exactly I made a mistake in the code
function z1=dcSQUID_diode_efficiency_animation
tic
phi0=linspace(0.000,2*pi,100);
theta0=linspace(0.000,2*pi,100);
flux1=linspace(0,1,10);
h = figure;
axis tight manual
filename = 'dc_squid_eff.gif';
for ii = 1:10
for i=1:100
for j=1:100
y=self11(phi0(i),theta0(j),flux1(ii));
eff(i,j,ii)=y;
end
end
[phi,theta]=meshgrid(phi0,theta0);
surfc(phi,theta,eff)
zlim([-0.5 0.5])
clim([-0.5 0.5])
colormap jet
colorbar('FontSize',34,'FontName','Times New Roman')
set(gca,'FontName','Times New Roman','FontSize',34)
xlabel('\phi','FontName','Times New Roman','fontsize',34,'fontweight','b');
ylabel('\theta','FontName','Times New Roman','fontsize',34,'fontweight','b');
set(gca,'XTick',0:pi/2:2*pi)
set(gca,'XTickLabel',{'0','\pi/2','\pi','3\pi/2','2\pi'})
set(gca,'YTick',0:pi/2:2*pi)
set(gca,'YTickLabel',{'0','\pi/2','\pi','3\pi/2','2\pi'})
drawnow
frame = getframe(h);
im = frame2im(frame);
[imind,cm] = rgb2ind(im,256);
if ii == 1
imwrite(imind,cm,filename,'gif', 'Loopcount',inf);
else
imwrite(imind,cm,filename,'gif','WriteMode','append');
end
end
function z2=self11(phi0,theta0,flux1)
rL1=1;
rL2=1;
rR0=1;
rR1=1;
rR2=1;
x = -2*pi:0.001:2*pi;
I_dc = cos(x./2).*atanh(sin(x./2))+rL1*cos((x+2*phi0)./2).*atanh(sin((x+2*phi0)./2))+rL2*cos((x+2*theta0)./2).*atanh(sin((x+2*theta0)./2))+...
rR0*cos((x-2*pi*flux1)./2).*atanh(sin((x-2*pi*flux1)./2))+rR1*cos(((x-2*pi*flux1)+2*phi0)./2).*atanh(sin(((x-2*pi*flux1)+2*phi0)./2))+...
rR2*cos(((x-2*pi*flux1)+2*theta0)./2).*atanh(sin(((x-2*pi*flux1)+2*theta0)./2));
I_min=min(I_dc);
I_max=max(I_dc);
z2=(I_max-abs(I_min))/(I_max+abs(I_min));
end
toc
end
댓글 수: 0
채택된 답변
Venkat Siddarth Reddy
2024년 5월 8일
편집: Venkat Siddarth Reddy
2024년 5월 8일
Hi,
The problem you're encountering originates from the 26th line of your code, specifically:
surfc(phi,theta,eff);
This error arises because the variable "eff" you are passing to the surfc function is three-dimensional, whereas the other parameters, "phi" and "theta," are two-dimensional. The discrepancy in dimensions between the parameters is what triggers the error.
Interestingly, the first plot might have successfully generated because, during the initial iteration, the dimensions of eff were (100,100,1). With only one element in the third dimension, eff default to a 2D array. However, as iterations progress and the size of eff increases, it no longer defaults to a 2D array, hence the error in subsequent iterations.
To resolve this issue, you should explicitly specify which slice of the "eff" matrix you intend to plot by using indexing:
surfc(phi,theta, eff(:,:,ii)) %assuming you want to use iith array in iith iteration
To learn more about the surfc function,refer to the following documentation:
Hope it helps!
추가 답변 (1개)
Tony
2024년 5월 8일
eff is three-dimensional, but the argument in surf must be two-dimensional. If you fix eff for a specific flux, that should work
surfc(phi,theta,eff(:,:,1))
참고 항목
카테고리
Help Center 및 File Exchange에서 Animation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!