首页 > 解决方案 > I'm getting an error "Undefined function 'Romberg' for input arguments of type 'char'." I'm trying to code the romberg integration method

问题描述

I'm trying to code the romberg integration method on matlab. I think I coded it right but I'm not getting why I have this 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)

This is its call

clc; clear all; close all;
a = 0;
b = pi;
nmax = 3;
fun ='sin (x)'  ;
[t, r]= Romberg (fun, a, b, nmax)

And this is the error: Undefined function 'Romberg' for input arguments of type 'char'.

标签: matlabnumerical-integration

解决方案


推荐阅读