How to fix errors within double integral.

1 visualización (últimos 30 días)
Amanda
Amanda el 30 de Oct. de 2022
Respondida: Carlos Guerrero García el 26 de Nov. de 2022
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)

Respuesta aceptada

Paul
Paul el 30 de Oct. de 2022
Editada: Paul el 30 de Oct. de 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 comentarios
Torsten
Torsten el 30 de Oct. de 2022
r = 3;
5 * pi*r^2
ans = 141.3717
Can you now imagine what the domain of integration is ?
Paul
Paul el 30 de Oct. de 2022
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.

Iniciar sesión para comentar.

Más respuestas (1)

Carlos Guerrero García
Carlos Guerrero García el 26 de Nov. de 2022
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 = 

Categorías

Más información sobre Matrix Computations en Help Center y File Exchange.

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by