How to fix errors within double integral.

1 回表示 (過去 30 日間)
Amanda
Amanda 2022 年 10 月 30 日
回答済み: Carlos Guerrero García 2022 年 11 月 26 日
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
Carlos Guerrero García 2022 年 11 月 26 日
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 Center および File ExchangeMatrix Computations についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by