How to fix errors within double integral.

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)

 Akzeptierte Antwort

Paul
Paul am 30 Okt. 2022
Bearbeitet: Paul am 30 Okt. 2022
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 Kommentare

Amanda
Amanda am 30 Okt. 2022
Hey Paul, thank you for the help. Do you happen to know how I would graph the region?
Paul
Paul am 30 Okt. 2022
You're welcome. What have you tried for the graph?
Amanda
Amanda am 30 Okt. 2022
Going to be honest, I am very new to this and don't even know where to start.
r = 3;
5 * pi*r^2
ans = 141.3717
Can you now imagine what the domain of integration is ?
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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

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 = 

Kategorien

Mehr zu Programming finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by