类型为“ double”的输入参数的未定义函数“ equation”

时间:2020-04-07 23:28:18

标签: matlab function octave newtons-method

写一个函数方程(M,epsilon,tol)来设置x = M + epsilon * sin(x)的解

功能

function x=newtonp(f, x, tol, h)
 
if nargin<4
   h=1e-8
end
 
if nargin<3
   tol=1e-8
end
 
while abs(f(x))>tol
   g=(f(x+h)-f(x))/h
   x=x-f(x)/g
end
end
 
function y=equation(M,epsilon,tol)
   y=M+epsilon*sin(x)-x
end

调用函数的代码:

例如:

newtonp(@equation(0.5,0.5,1e-5),2,1e-5,1e-8)

然后我得到类型为'double'的输入参数的未定义函数'equation'。,但我不知道为什么。有人可以向我解释吗?

编辑:

使用FangQ的答案,我有

function y=equation(M,epsilon,tol)
y.newtonp=@computeNewtonp
y.sin=@computeSin
end
 
function x=computeNewtonp(f,x,tol,h)
if nargin<4
h=1e-8
end
if nargin<3
tol=1e-8
end
fx=f(x)
while abs(fx)>tol
g=(f(x+h)-fx)/h
x=x-fx/g
abs(fx)
end
end
 
function z=computeSin(x,epsilon,M)
x=computeNewtonp(f,x,tol,h)
z=epsilon*sin(x)-x+M
end

但是我有:变量y的数据类型必须为double。当前为struct类型。检查变量在何处分配了值

2 个答案:

答案 0 :(得分:1)

如果您在功能单元内编写函数,则称为局部函数

https://www.mathworks.com/help/matlab/ref/localfunctions.html

此功能仅对第一个功能可见,但是您可以通过使主功能返回一个手柄来使其可见,如本教程中一样

https://www.mathworks.com/help/matlab/matlab_prog/call-local-functions-using-function-handles.html

答案 1 :(得分:0)

我认为当您想在equation中使用x时,可能需要将newtonp定义为function y=equation(x,M,epsilon,tol) y=M+epsilon*sin(x)-x; end newtonp(@(x) equation(x,0.5,0.5,1e-5),2,1e-5,1e-8) % use anonymous function 的功能:

ans =  0.88786

您将获得

cout << "composite array test:" << endl; 
       int size = 27; 
       SpamValue** spamCompositesArray =  new SpamValue*[size];
        /*spamBool*/
        spamCompositesArray[0] = new SpamValue(spamBool);
        spamCompositesArray[0]->setValueBool(0);
        spamCompositesArray[1] = new SpamValue(spamBool);
        spamCompositesArray[1]->setValueBool(1);
        /*spamByte*/
        spamCompositesArray[2] = new SpamValue(spamByte);
         spamCompositesArray[2]->setValueByte(1);
        spamCompositesArray[3] = new SpamValue(spamByte);
         spamCompositesArray[3]->setValueByte(0);
         /*spamChar*/
        spamCompositesArray[4] = new SpamValue(spamChar);
        spamCompositesArray[4]->setValueChar('a');
        spamCompositesArray[5] = new SpamValue(spamChar);
        spamCompositesArray[5]->setValueChar('b');
        spamCompositesArray[6] = new SpamValue(spamChar);
        spamCompositesArray[6]->setValueChar('c');
        spamCompositesArray[7] = new SpamValue(spamChar);
        spamCompositesArray[7]->setValueChar('d');
        /*spamInt*/
        spamCompositesArray[8] = new SpamValue(spamInt);
        spamCompositesArray[8]->setValueInt(1);
        spamCompositesArray[9] = new SpamValue(spamInt);
        spamCompositesArray[9]->setValueInt(2);
        spamCompositesArray[10] = new SpamValue(spamInt);
        spamCompositesArray[10]->setValueInt(3);
        spamCompositesArray[11] = new SpamValue(spamInt);
        spamCompositesArray[11]->setValueInt(4);
        /*spamFloat*/
        spamCompositesArray[12] = new SpamValue(spamFloat);
        spamCompositesArray[12]->setValueFloat(1);
        spamCompositesArray[13] = new SpamValue(spamFloat);
        spamCompositesArray[13]->setValueFloat(2);
        spamCompositesArray[14] = new SpamValue(spamFloat);
         spamCompositesArray[14]->setValueFloat(3);
        spamCompositesArray[15] = new SpamValue(spamFloat);
         spamCompositesArray[15]->setValueFloat(4);
        /*spamDouble*/
        spamCompositesArray[16] = new SpamValue(spamDouble);
        spamCompositesArray[16]->setValueDouble(1);
        spamCompositesArray[17] = new SpamValue(spamDouble);
         spamCompositesArray[17]->setValueDouble(2);
        spamCompositesArray[18] = new SpamValue(spamDouble);
         spamCompositesArray[18]->setValueDouble(3);
        spamCompositesArray[19] = new SpamValue(spamDouble);
         spamCompositesArray[19]->setValueDouble(4);
        /*String*/
        spamCompositesArray[20] = new SpamValue(spamString); 
        const char* mercury = "mercury";
        spamCompositesArray[20]->setValueString(strdup(mercury));
        spamCompositesArray[21] = new SpamValue(spamString);
         const char* venus = "venus";
        spamCompositesArray[21]->setValueString(strdup(venus));
        spamCompositesArray[22] = new SpamValue(spamString);
         const char* earth = "earth";
        spamCompositesArray[22]->setValueString(strdup(earth));
        spamCompositesArray[23] = new SpamValue(spamString);
         const char* mars = "mars";
        spamCompositesArray[23]->setValueString(strdup(mars));
        /*spamShort*/
        spamCompositesArray[24] = new SpamValue(spamShort);
         spamCompositesArray[24]->setValueShort(0);
        spamCompositesArray[25] = new SpamValue(spamShort);
          spamCompositesArray[25]->setValueShort(1);
        spamCompositesArray[26] = new SpamValue(spamShort);
          spamCompositesArray[26]->setValueShort(2);


        for (int i=0; i<size; i++)
        {
            switch (spamCompositesArray[i]->getType())
           {
               case spamBool:
                   cout << "spamBool:" << endl; 
                   cout << spamCompositesArray[i]->getValueBool() << endl; 
                   break; 
               case spamChar:
                     cout << "spamChar:" << endl; 
                     cout << spamCompositesArray[i]->getValueChar() << endl; 
                   break; 
               case spamByte:
                     cout << "spamByte:" << endl; 
                     cout << spamCompositesArray[i]->getValueByte() << endl; 
                   break; 
               case spamInt:
                     cout << "spamInt" << endl; 
                     cout << spamCompositesArray[i]->getValueInt() << endl; 
                   break;
               case spamShort:
                       cout << "spamShort" << endl; 
                       cout << spamCompositesArray[i]->getValueShort() << endl; 
                   break; 
               case spamFloat:  
                   cout << "spamFloat" << endl; 
                   cout << spamCompositesArray[i]->getValueFloat() << endl;
                   break;
                case spamDouble:  
                     cout << "spamDouble" << endl; 
                     cout << spamCompositesArray[i]->getValueDouble() << endl;
                   break;
                case spamString:
                     cout << "spamString" << endl; 
                     cout << spamCompositesArray[i]->getValueString() << endl;
                break; 
            default:
                break; 
        }