gpt4 book ai didi

matlab - 如何在 MATLAB 的 for 循环中调用函数?

转载 作者:行者123 更新时间:2023-12-01 08:07:05 34 4
gpt4 key购买 nike

我想像这样在循环中调用(执行)一个 m 文件(函数):

global m, r
m = 2;
for n = 2:10;
for r1 = 0:n-m;
r2 = n-m-r1;
r = [r1,r2];
[Call the function here?????????]
end
end

这是函数:

function main
x0 = [-0.5403,0.5471];
fsolve(@fcn,x0)

function z = fcn(X)
rand('twister',5409);
global m, r
a = rand(m,1);
for i = 1:m
sm(i) = 0.0;
for l = m-i+1:m
sm(i) = sm(i)+r(l);
end
s = 1.0/(i+sm(i));
g(i) = (a(i))^s;
end
prod = 1.0;
for k = 1:m
prod = prod * g(m+1-k);
u(k) = 1.0-prod;
x(k) = (sqrt(3)/pi)*log(u(k)/(1-u(k)));
end
sum = 0;
sum1 = 0;
sum2 = 0;
for j = 1:m
sum = sum+(r(j)+2)*(1/(1+exp((-pi/sqrt(3))*((x(j)-X(1))/X(2)))));
sum1 = sum1+(r(j)+2)*((x(j)-X(1))/X(2))*(1/(1+exp((-pi/sqrt(3))*((x(j)-X(1))/X(2)))));
sum2 = sum2+(x(j)-X(1))/X(2);
end
z(1) = pi/(X(2)*sqrt(3))*(-m+sum);
z(2) =(-1/X(2))*(m+(pi/sqrt(3))*(sum2-sum1));

非常感谢您的帮助。

最佳答案

函数mainfcn 应该保存在名为“main.m”的文件中。您必须确保此文件位于您当前的工作目录或 somewhere on the MATLAB path (如 Amro 的评论中所述),以便 MATLAB 可以使用它。由于 main 不需要输入参数也没有输出参数,因此您可以通过以下任何一种方式调用它:

main
main;
main()
main();

关于matlab - 如何在 MATLAB 的 for 循环中调用函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1567621/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com