How to fix errors within double integral.

1 次查看(过去 30 天)
I need to graph the region. This is the code I started with
fun = @(x,y) 5
xmin = -3
xmax= 3
ymin = @(x) -1.*sqrt(9- x.^2)
ymax = @(x) sqrt(9- x.^2);
q = integral2(fun,xmin,xmax,ymin,ymax)
Whenever I try to run this I get several error meassages.
Error using integral2Calc>integral2t/tensor (line 241)
Integrand output size does not match the input size.
Error in integral2Calc>integral2t (line 55)
[Qsub,esub] = tensor(thetaL,thetaR,phiB,phiT);
Error in integral2Calc (line 9)
[q,errbnd] =
integral2t(fun,xmin,xmax,ymin,ymax,optionstruct);
Error in integral2 (line 105)
Q =
integral2Calc(fun,xmin,xmax,yminfun,ymaxfun,opstruct);
Error in math241_project2 (line 7)
q = integral2(fun,xmin,xmax,ymin,ymax)

采纳的回答

Paul
Paul 2022-10-30
编辑:Paul 2022-10-30
Hi @Amanda,
The doc page for integral2 say that the function that defines the integrand "must accept two arrays of the same size and return an array of corresponding values." However, fun in the Question always returns a scalar for any pair of inputs. Correct fun as shown below so it returns an array the same size as x with all elements equal to 5
fun = @(x,y) 5*ones(size(x));
xmin = -3;
xmax = 3;
ymin = @(x) -1.*sqrt(9 - x.^2);
ymax = @(x) sqrt(9 - x.^2);
q = integral2(fun,xmin,xmax,ymin,ymax)
q = 141.3717
  5 个评论
Torsten
Torsten 2022-10-30
r = 3;
5 * pi*r^2
ans = 141.3717
Can you now imagine what the domain of integration is ?
Paul
Paul 2022-10-30
Suppose I have a function defined like this
y = @(x) x.^2;
And I want to plot it between
xmin = -5;
xmax = 5;
Then I can make a plot like this
x = xmin:0.01:xmax;
plot(x,y(x))
If I want to make the apsect ratio 1:1
axis equal
Check hold to learn one way to add an additional line to the plot.

请先登录,再进行评论。

更多回答(1 个)

Carlos Guerrero García
For plotting the region, I suggest the following code:
x=-3:0.05:3; % Setting up the range of X
y=sqrt(9-x.^2); % Calculating the Y values
fill([x flip(x)],[y -flip(y)],'b','FaceAlpha',0.5); % [x,flip(x)] for forwards/towards advance in X and mapping [y -flip(y)]
% In the preceding line 'b' is for a blue drawing, and setting 'FaceAlpha' to 1/2 for trasparency
grid on; % plotting the grid
axis equal; axis([-3 3 -3 3]) % Setting up a nice view
And the following code determines the value of the integral in the statement:
syms x;
int(int(5,-sqrt(9-x^2),sqrt(9-x^2)),-3,3)
ans = 

类别

Help CenterFile Exchange 中查找有关 Matrix Computations 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by