I'm getting an error "Undefined function 'Romberg' for input arguments of type 'char'." I'm trying to code the romberg integration method.

2 views (last 30 days)
I'm trying to code the romberg integration method. I think I coded it right but I'm not getting why I have thi error.
This is the function.
function[t , r] = Romberg (fun, a, b, nmax)
f = inline(fun);
r(1, 1) = (b - a) * (f(a) + f(b)) / 2;
for i = 1 : nmax
h(i) = (b-a) /2^(i) ;
m = 0;
for k = 1 : (2^(i))-1
m = m + f (a+k*h(i));
end
r(i + 1, 1) = (h(i) / 2) * (f(a) + f(b) + 2*m);
for j = 2 : i
r(i, j) = r(i, j-1) + (r(i, j-1) - r(i - 1, j - 1)) / (4^(j-1) - 1);
end
end
t = r (i, j)
And this is it's call.
clc; clear all; close all;
a = 0;
b = pi;
nmax = 3;
fun ='sin (x)' ;
[t, r]= Romberg (fun, a, b, nmax)
This is the error I'm getting.
Undefined function 'Romberg' for input arguments of type 'char'.

Answers (1)

Stephan
Stephan on 24 Nov 2020
For me it worked:
a = 0;
b = pi;
nmax = 3;
fun ='sin (x)' ;
[t, r]= Romberg (fun, a, b, nmax)
function[t , r] = Romberg (fun, a, b, nmax)
f = inline(fun);
r(1, 1) = (b - a) * (f(a) + f(b)) / 2;
for i = 1 : nmax
h(i) = (b-a) /2^(i) ;
m = 0;
for k = 1 : (2^(i))-1
m = m + f (a+k*h(i));
end
r(i + 1, 1) = (h(i) / 2) * (f(a) + f(b) + 2*m);
for j = 2 : i
r(i, j) = r(i, j-1) + (r(i, j-1) - r(i - 1, j - 1)) / (4^(j-1) - 1);
end
end
t = r (i, j)
end
gives:
t =
1.9986
t =
1.9986
r =
0.0000 0 0
1.5708 2.0944 0
1.8961 2.0046 1.9986
1.9742 0 0
  6 Comments
peter el murr
peter el murr on 24 Nov 2020
Edited: peter el murr on 28 Nov 2020
Now it is giving me this error:
Undefined function 'Romberg' for input arguments of type 'function_handle'.

Sign in to comment.

Categories

Find more on Function Creation 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!