如何迭代函数? [英] How to iterate over functions?
本文介绍了如何迭代函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
v = 1;
fun = @root;
x0 = [0,0]
options = optimset('MaxFunEvals',100000,'MaxIter',10000);
x = fsolve(fun,x0,options)
另外,我有以下功能在一个单独的文件中:
函数D = root(x)
v = 1;
D(1)= x(1)+ x(2)+ v - 2;
D(2)= x(1) - x(2)+ v - 1.8;
end
现在,我想在 v = sort(rand(1,1000))
。换句话说,我想迭代 v
的每个值的函数。
解决方案你需要修改
root
来接受一个额外的变量( v
),然后改变函数处理根目录到一个匿名函数,它在 v
中提供你要的 <$ c $函数D = root(x,v)
D(1)= x(1)+ x(2)+ v - 2;
D(2)= x(1) - x(2)+ v - 1.8;
end
%使用特定的值v $ b $创建一个函数句柄fun = @(x)root(x,v(k))
I would like to apply loop over a function. I have the following "mother" code:
v = 1;
fun = @root;
x0 = [0,0]
options = optimset('MaxFunEvals',100000,'MaxIter', 10000 );
x = fsolve(fun,x0, options)
In addition, I have the following function in a separate file:
function D = root(x)
v = 1;
D(1) = x(1) + x(2) + v - 2;
D(2) = x(1) - x(2) + v - 1.8;
end
Now, I would like to find roots when v = sort(rand(1,1000))
. In other words, I would like to iterate over function for each values of v
.
解决方案
You will need to modify root
to accept an additional variable (v
) and then change the function handle to root to an anonymous function which feeds in the v
that you want
function D = root(x, v)
D(1) = x(1) + x(2) + v - 2;
D(2) = x(1) - x(2) + v - 1.8;
end
% Creates a function handle to root using a specific value of v
fun = @(x)root(x, v(k))
这篇关于如何迭代函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文