My function of L-norm doesn't work

3 views (last 30 days)
Do
Do on 12 Dec 2023
Commented: Dyuman Joshi on 15 Mar 2024
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?

Answers (1)

Dyuman Joshi
Dyuman Joshi on 12 Dec 2023
Edited: Dyuman Joshi on 25 Feb 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

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!