My function of L-norm doesn't work

I wrote a function file that calculates L^2 norm of the difference between 2 functions at the certain area:
function l = Lnorm(f1,f2)
l = sqrt(integral(@(x) (f1 - f2).^2, -8/5, 8/7));
end
Then i tested it with the code below:
f1 = x.^2;
f2 = x.^4;
Lnorm(f1, f2)
It sent me an error message: Error using integralCalc/finalInputChecks
Output of the function must be the same size as the input. If FUN is an array-valued integrand,
set the 'ArrayValued' option to true"
How to fix this?

Respuestas (1)

Dyuman Joshi
Dyuman Joshi el 12 de Dic. de 2023
Editada: Dyuman Joshi el 25 de Feb. de 2024
f1 and f2 need to be defined as function handles.Notice the updated syntax used in the integrand to integral() as well -
f1 = @(x) x.^2;
f2 = @(x) x.^4;
Lnorm(f1, f2)
ans = 1.4474
function l = Lnorm(f1,f2)
l = sqrt(integral(@(x) (f1(x) - f2(x)).^2, -8/5, 8/7));
end

Etiquetas

Preguntada:

Do
el 12 de Dic. de 2023

Comentada:

el 15 de Mzo. de 2024

Community Treasure Hunt

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

Start Hunting!

Translated by