找到“y=x*x”的最小值在Matlab中使用遗传算法
有人可以帮我解决这个问题吗?我是 Matlab 新手...对我来说,理解如何在 Matlab 中创建和使用遗传算法有点困难。 如果有人可以帮助编写一些非常简单的代码来搜索指定函数的最小值/最大值。 我读到 gatool 应该用于此目的...但我无法理解 Matlab 帮助网络的示例。我正在执行后续步骤:
在文本编辑器中,我正在输入下一个:
函数 y= 抛物线(x) y=x*x; 结尾
然后我启动
GATOOL
并指定此函数,如@parabola
< /p>- 设置变量数量(等于 2)
Initial range = [-10;10]
。其他参数设置为默认
当我按
<块引用>开始
按钮时,我看到结果:fitnessfcn 错误:输入参数“x”未定义。
Would somebody help me please in this question. I'm new in Matlab... And it's a bit hard for me to understand how to create and use genetic algorithm in Matlab.
If anybody could help to write some very simple code for searching minimum/maximum of specified function.
I read that the gatool should be used for that... but I can't understand the examples of Matlab help network. I'm doing the next steps:
In text editor I'm typing the next:
function y= parabola(x) y=x*x; end
Then I'm launching the
GATOOL
and specifying this function like@parabola
- Setting the number of variables (equals 2)
Initial range = [-10;10]
.The other parameters are set as Default
When I press
Start
Button I see a result:Error in fitnessfcn: Input argument "x" is undefined.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
主要问题是您不了解工具箱是如何工作的。您应该参考文档来了解整个想法。
因此,适应度函数应该是一个 函数句柄 并且应该返回一个标量。
首先,你的功能没有明确定义。如果你想定义一个匿名函数,你应该
使用 GA 的 GUI 来实现同样的功能。如果您想在
m
文件中定义函数,您应该具有如下内容:parabola.m
并且定义句柄,如
fh = @parabola.在上面的代码中,您将
parabola
替换为新句柄fh
。我希望这可以帮助您入门。
The main problem is that you don't understand how the toolbox works. You should refer to the documentation to get the whole idea.
So, the fitness function should be a function handle and should return a scalar.
First, your function is not well defined. If you want to define an anonymous function you should
The same can be achieved with the GUI of GA. In case you want to define your function in an
m
file you should have something like:parabola.m
And you define the handle like
fh = @parabola
. And in the code above you replaceparabola
for the new handle,fh
.I hope this help you get started.