Using an integral in a function

1 visualización (últimos 30 días)
Yu Haobo Yu
Yu Haobo Yu el 21 de Oct. de 2020
Comentada: Yu Haobo Yu el 21 de Oct. de 2020
Hi, I have a problem as follows:
In the main file, I define a function f_1 and a number q_Max:
q_Max=10;
f_1 = @(q) (0).*((q<0)|(q>q_Max)) + (q*height/peak_1).*((0<=q) & (q<=peak_1)) +...
(height-(q-peak_1)*height/(q_Max-peak_1)).*((peak_1<q) & (q<=q_Max));
In a function file, I define the following function:
function result = Lambda_1(beta)
global f_1 q_Max
result = beta*integral(@(q) f_1(q).*q.^2,0,q_Max);
end
In the main file, I run the following line and get an error:
Lambda_1(0.1)
Error using integral (line 85)
A and B must be floating-point scalars.
Error in Lambda_1 (line 10)
result = beta*integral(@(q) f_1(q).*q.^2,0,q_Max);
I wonder why this happens. I think both q and q_Max are scalars. Thanks in advance.

Respuesta aceptada

Stephen23
Stephen23 el 21 de Oct. de 2020
The problem is that in the "main file" you did not declare q_Max and f_1 as global.
But rather than using global variables (which should be avoided) you would be much better of parameterizing the function using one of the recommended syntaxes:

Más respuestas (0)

Categorías

Más información sobre Just for fun en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by