sum in function of function itself (recursive)

I have following Problem:
I want to calculate an adaptive Quadrature recursively. My Quadratur formula U is given by two funcitons S and T.
This is my code, where a and b are the endpoints of my interval, e is the error tolerance, and f is the function:
function y=adaptive_quadratur(a,b,e,f)
S=((b-a)/6)*(f(a)+4*f((a+b)/2)+f(b))
T=(b-a)/12*(f(a)+4*f((3*a+b)/4)+2*f((a+b)/2)+4*f((a+3*b)/4)+f(b))
U=T+(T-S)/15
if abs(T-S)<=e*(b-a)
y=U;
return
end
adaptive_quadratur(a,(a+b)/2,e,f) + adaptive_quadratur((a+b)/2,b,e,f)
end
However Matlab gives me an Error for my last line, but I dont know why.

 Akzeptierte Antwort

Torsten
Torsten am 14 Dez. 2022
Bearbeitet: Torsten am 14 Dez. 2022
Works for me:
f = @(x)x^2;
a = 0;
b = 1;
e = 1e-4;
y = adaptive_quadratur(a,b,e,f)
y = 0.3333
function y=adaptive_quadratur(a,b,e,f)
S=((b-a)/6)*(f(a)+4*f((a+b)/2)+f(b));
T=(b-a)/12*(f(a)+4*f((3*a+b)/4)+2*f((a+b)/2)+4*f((a+3*b)/4)+f(b));
U=T+(T-S)/15;
if abs(T-S)<=e*(b-a)
y=U;
return
end
y = adaptive_quadratur(a,(a+b)/2,e,f) + adaptive_quadratur((a+b)/2,b,e,f)
end

1 Kommentar

Thank you, I also just solved my issue using ChatGPT. I was missing an "y=" in front of my last line.
So this would be my working code:
function y=adaptive_quadratur(a,b,e,f)
S=((b-a)/6)*(f(a)+4*f((a+b)/2)+f(b));
T=(b-a)/12*(f(a)+4*f((3*a+b)/4)+2*f((a+b)/2)+4*f((a+3*b)/4)+f(b));
U=T+(T-S)/15
if abs(T-S)<=e*(b-a)
y=U;
return
end
y=adaptive_quadratur(a,(a+b)/2,e,f) + adaptive_quadratur((a+b)/2,b,e,f)
end

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Startup and Shutdown finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2022b

Gefragt:

am 14 Dez. 2022

Bearbeitet:

am 14 Dez. 2022

Community Treasure Hunt

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

Start Hunting!

Translated by