循环更改Matlab函数 [英] Change Matlab function in loop
问题描述
我想在for循环的每个步骤中对MATLAB函数进行一些更改.我的函数太复杂了,无法以匿名方式编写.有什么方法可以在每个步骤中更改m文件功能吗?
I want to make a slight change to a MATLAB function at each step in a for loop. My function is too complicated to write as anonymous. Is there any way to change an m-file function at each step?
其他信息: 我的函数是一个有8个输入和无限多个解决方案的方程式.我想要设置7个输入,然后使用fsolve查找第8个.在for循环中更改这7个固定输入中的一些,以便我可以创建该方程解的图.
Additional Info: My function is an equations with 8 inputs and infinitely many solutions. I want set 7 of the inputs and then use fsolve to find the 8th. vary some of these 7 fixed inputs in a for loop so that I can create a graph of the solutions to this equation.
推荐答案
让我们举一个包含两个输入的示例,您要更改其中的一个.由于您声称您的函数确实很复杂,因此让我们将其写入一个名为complicated.m
的文件中,该文件将保存在Matlab路径中.
Let's make an example with two inputs, of which you want to change one. Since you claim your function is really complicated, let's write it into a file called complicated.m
, which we save on the Matlab path.
function out = complicated(v1,v2,x)
out = v1*x-v2*x.^2;
假设我们要在循环的每次迭代中更改v1
和v2
并找到多项式的根并将其绘制
Say we want to change v1
and v2
at every iteration in the loop and find a root of the polynomial and plot it
figure,hold on
for v1 = 1:5
for v2 = 1:5
%# define the function
myFun = @(x)complicated(v1,v2,x);
%# find the roots
fzero(myFun,1)
%# plot the function
plot(-5:0.1:5,myFun(-5:0.1:5))
end
end
这篇关于循环更改Matlab函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!