Trouble plotting 2D and 3D models of data.

2 ビュー (過去 30 日間)
Yianni
Yianni 2014 年 10 月 26 日
回答済み: Mike Garrity 2014 年 10 月 27 日
%I am having trouble plotting both 2D and 3D graphs on Matlab. I am confident that my variables (as far as I know) are working just fine. However I keep getting this message:
"Index exceeds matrix dimensions.
Error in Problem_4 (line 77)
h(j) = plot(x,T(:,j),scm(j,:),'LineWidth',2); hold on"
%What can I do to fix this? (Script below)
clear all, close all, nfig = 0;
%Data in Problem
S = 1200; %Internal Heating (W/m^3) To = 40; %Initial Temperature (C) k = 0.16; %Thermal Conductivity (W/(m*c)) HH = 16; H = 16*(0.0254); %Height of long wooden beam (converted to m) WW = 10; W = 10*(0.0254); %Width of long wooden beam (converted to m) Ly = H/2; %Height of Upper Right Quadrant of beam (m) Lx = W/2; %Width of Upper Right Quadrant of beam (m) Nx = 51; %Number of x values x = linspace(0, Lx, Nx)'; %Vector of points to evaluate function nmax = 20; %Max # of nonzero terms in expansion tol = 0.001; %Tolerance to stop series evaluation
%Calculations of Ln and Bn Ln = zeros(1,nmax); Bn = zeros(1,nmax);
for n = 1:nmax Ln(n)= ((2*n-1)*pi)/(2*Lx); Bn(n) = ((2*S)/(k*Lx))*((-1^n)/((Ln(n)^3)*cosh(Ln(n)*Ly))); end
%Choose which plot to be produced
ic = menu('Choose type of plot', ... 'Show T(x) for several different values of y (2-D plot) ', ... 'Show T(x,y) in a variety of surface plots (3-D plots) ');
%Series Expansion for Several Different Values of y
if ic == 1 yy = linspace(0,Ly,51); %For 2-D Plot else yy = linspace(0,Ly,61); %For 3-D Plot end
Ny = length(yy); T = zeros(Nx,Ny);
%Set Initial Temperature Profile
T(:,1) = To*ones(size(x));
%Loop Over Remaining y Values
for j = 1:Ny y = yy(j); mrerr = 1.0; n = 0; Tx = zeros(size(x)); while mrerr > tol && n < nmax n = n+1; Txy = Bn(n)*cos(Ln(n)*x)*cosh(Ln(n)*y)+To+... (S/(2*k))*(Lx.^2-x.^2); Tx = Tx + Txy; i = find(Tx); rerr = Txy(i)./Tx(i); mrerr = max(abs(rerr)); end T(:,j) = Tx; disp([' Needed ',num2str(n),' terms for convergence at y = ',... num2str(y)]) end
%2-D plot
if ic == 1
%Color and marker code for creating 2-D plots
Ncm = 6; scm = ['r-';'g:';'b-';'m:';'c-';'k:'];
h = zeros(Ny,1); st = zeros(Ny,9);
%Plot curves of T(x,y) for various y values
nfig = nfig+1; figure(nfig)
for j = 1:Ny
h(j) = plot(x,T(:,j),scm(j,:),'LineWidth',2); hold on
st(j,:) = sprintf('%5.1f sec',yy(j));
end
title('2-D Temperature Distribution of Long Wooden Beam')
grid,xlabel('X Values'),ylabel('Temperature (C)')
legend(h,char(st),'Location','SouthEast')
end
%Various 3-D plots
if ic == 2
nfig = nfig+1; figure(nfig); set(gcf,'renderer','zbuffer')
surf(yy,x,T), shading interp, colorbar, view(60,30), hold on
[cc,hh] = contour3(yy,x,T, [80 60 40 20 10 5]);
clabel(cc), set(hh,'EdgeColor','k')
axis('tight')
title('3-D Temperature Distribution of Long Wooden Beam')
ylabel('X Value'), xlabel('Y Value')
zlabel('Temperature (C)'), hold off
nfig = nfig+1; figure(nfig); set(gcf,'renderer','zbuffer')
surf(yy,x,T), shading interp, colorbar, view(2), hold on
[cc,hh] = contour3(yy,x,T, [80 60 40 20 10 5]);
clabel(cc), set(hh,'EdgeColor','k')
axis('tight')
title('3-D Temperature Distribution of Long Wooden Beam')
ylabel('X Value'), xlabel('Y Value')
zlabel('Temperature (C)'), gtext('Temperature (C)'), hold off
end

採用された回答

Mike Garrity
Mike Garrity 2014 年 10 月 27 日
It's trying to say that you've indexed off the end of your array of linestyles. The variable scm contains 6 linestyles. When you go around the loop the 7th time, you get this message. You can either add some more linestyles to scm or use the mod function to index into it with a repeat.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeFormula Manipulation and Simplification についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by