I am getting "Array indices must be positive integers or logical values" error when viewsolid is used. What does this mean and how to solve it?
48 ビュー (過去 30 日間)
古いコメントを表示
clc
clear all
syms x y z
vol = int(int(x^2+y^2+1,x,0,sqrt(1+x^2)),y,0,1)
viewSolid(z,0+0*x*y, x^2+y,x,0,sqrt(1+x^2),y,0,1);
grid on;
回答 (1 件)
Walter Roberson
2024 年 10 月 27 日 20:37
You have
viewSolid(z,0+0*x*y, x^2+y,x,0,sqrt(1+x^2),y,0,1);
The fifth input is numeric 0.
viewSolid is designed to test for the fifth input being symbolic, and if so assumes that the sixth input is symbolic as well. But if the fifth input is not symbolic then it assumes that the sixth input is a function handle.
You need to call
viewSolid(z,0+0*x*y, x^2+y,x,sym(0),sqrt(1+x^2),y,0,1);
2 件のコメント
Torsten
2024 年 10 月 28 日 9:19
編集済み: Torsten
2024 年 10 月 28 日 9:35
The limits of the solid with respect to x are not allowed to depend on x:
viewSolid(z,0+0*x*y, x^2+y^2,x,sym(0),sqrt(1+x^2),y,0,1);
Maybe you mean:
clc
clear all
syms x y z
vol = int(int(x^2+y^2+1,x,0,sqrt(1+x^2)),y,0,1)
viewSolid(z,0+0*x*y, x^2+y,y,sym('0'),sqrt(1+x^2),x,0,1);
grid on;
function viewSolid(zvar, F, G, yvar, f, g, xvar, a, b)
%VIEWSOLID is a version for MATLAB of the routine on page 161
% of "Multivariable Calculus and Mathematica" for viewing the region
% bounded by two surfaces for the purpose of setting up triple integrals.
% The arguments are entered from the inside out.
% There are two forms of the command --- either f, g,
% F, and G can be vectorized functions, or else they can
% be symbolic expressions. xvar, yvar, and zvar can be
% either symbolic variables or strings.
% The variable xvar (x, for example) is on the
% OUTSIDE of the triple integral, and goes between CONSTANT limits a and b.
% The variable yvar goes in the MIDDLE of the triple integral, and goes
% between limits which must be expressions in one variable [xvar].
% The variable zvar goes in the INSIDE of the triple integral, and goes
% between limits which must be expressions in two
% variables [xvar and yvar]. The lower surface is plotted in red, the
% upper one in blue, and the "hatching" in cyan.
%
% Examples: viewSolid(z, 0, (x+y)/4, y, x/2, x, x, 1, 2)
% gives the picture on page 163 of "Multivariable Calculus and Mathematica"
% and the picture on page 164 of "Multivariable Calculus and Mathematica"
% can be produced by
% viewSolid(z, x^2+3*y^2, 4-y^2, y, -sqrt(4-x^2)/2, sqrt(4-x^2)/2, ...
% x, -2, 2,)
% One can also type viewSolid('z', @(x,y) 0, ...
% @(x,y)(x+y)/4, 'y', @(x) x/2, @(x) x, 'x', 1, 2)
%
if isa(f, 'sym') % case of symbolic input
ffun=inline(vectorize(f+0*xvar),char(xvar));
gfun=inline(vectorize(g+0*xvar),char(xvar));
Ffun=inline(vectorize(F+0*xvar),char(xvar),char(yvar));
Gfun=inline(vectorize(G+0*xvar),char(xvar),char(yvar));
oldviewSolid(char(xvar), double(a), double(b), ...
char(yvar), ffun, gfun, char(zvar), Ffun, Gfun)
else
oldviewSolid(char(xvar), double(a), double(b), ...
char(yvar), f, g, char(zvar), F, G)
end
end
%%%%%%% subfunction goes here %%%%%%
function oldviewSolid(xvar, a, b, yvar, f, g, zvar, F, G)
for counter=0:20
xx = a + (counter/20)*(b-a);
YY = f(xx)*ones(1, 21)+((g(xx)-f(xx))/20)*(0:20);
XX = xx*ones(1, 21);
%% The next lines inserted to make bounding curves thicker.
widthpar=0.5;
if counter==0, widthpar=2; end
if counter==20, widthpar=2; end
%% Plot curves of constant x on surface patches.
plot3(XX, YY, F(XX, YY).*ones(1,21), 'r', 'LineWidth', widthpar);
hold on
plot3(XX, YY, G(XX, YY).*ones(1,21), 'b', 'LineWidth', widthpar);
end;
%% Now do the same thing in the other direction.
XX = a*ones(1, 21)+((b-a)/20)*(0:20);
%% Normalize sizes of vectors.
YY=0:2; ZZ1=0:20; ZZ2=0:20;
for counter=0:20,
%% The next lines inserted to make bounding curves thicker.
widthpar=0.5;
if counter==0, widthpar=2; end
if counter==20, widthpar=2; end
for i=1:21,
YY(i)=f(XX(i))+(counter/20)*(g(XX(i))-f(XX(i)));
ZZ1(i)=F(XX(i),YY(i));
ZZ2(i)=G(XX(i),YY(i));
end;
plot3(XX, YY, ZZ1, 'r', 'LineWidth',widthpar);
plot3(XX, YY, ZZ2, 'b', 'LineWidth',widthpar);
end;
%% Now plot vertical lines.
for u = 0:0.2:1,
for v = 0:0.2:1,
x=a + (b-a)*u; y = f(a + (b-a)*u) +(g(a + (b-a)*u)-f(a + (b-a)*u))*v;
plot3([x, x], [y, y], [F(x,y), G(x, y)], 'c');
end;
end;
xlabel(xvar)
ylabel(yvar)
zlabel(zvar)
hold off
end
参考
カテゴリ
Help Center および File Exchange で Symbolic Variables, Expressions, Functions, and Preferences についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!