函数嵌套报错。

조회 수: 1 (최근 30일)
eakpwsv
eakpwsv 2023년 5월 22일
답변: xaetsypn 2023년 5월 22일
function [funstr,y] = test_function(x,FunNum)
funstr = str2func(['test',num2str(FunNum)]);
y = funstr(x);
% =========================================================================
% 测试函数集
%==========================================================================
function y = test1(x)
[~,Ns]=size(x);
y=zeros(1,Ns);
for i=1:Ns
x1=x(1,i);
x2=x(2,i);
f=( 1+(x1+x2+1)^2 * (19-14*x1+3*x1^2-14*x2+6*x1*x2+3*x2^2) )...
*( 30 + (2*x1-3*x2)^2 * (18-32*x1+12*x1^2+48*x2-36*x1*x2+27*x2^2) );
y(1,i)=f;
end
end
end
脚本:
clear;clc
[funstr,y] = test_function(rand(2,100),1)
运行不出来,求大佬指教

채택된 답변

xaetsypn
xaetsypn 2023년 5월 22일
仅供参考
clear;clc
[funstr,y] = test_function(rand(2,100),1)
function [funstr,y] = test_function(x,FunNum)
funstr = str2func(['test',num2str(FunNum)]);
y = funstr(x);
% =========================================================================
%                             测试函数集
%==========================================================================
   
end
function y = test1(x)
        [~,Ns]=size(x);
        y=zeros(1,Ns);
        for i=1:Ns
            x1=x(1,i);
            x2=x(2,i);
            f=( 1+(x1+x2+1)^2 * (19-14*x1+3*x1^2-14*x2+6*x1*x2+3*x2^2) )...
                *( 30 + (2*x1-3*x2)^2 * (18-32*x1+12*x1^2+48*x2-36*x1*x2+27*x2^2) );
            y(1,i)=f;
        end
    end

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Simulation, Tuning, and Visualization에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!