当前位置: 首页 > news >正文

matlab小记(四)

在matlab中如何给目标函数传送独立的变量参数。

Passing Extra Parameters

 

Extra Parameters, Fixed Variables, or Data

Sometimes objective or constraint functions have parameters in addition to the independent variable. The extra parameters can be data, or can represent variables that do not change during the optimization. There are three methods of passing these parameters:

  • Anonymous Functions

  • Nested Functions

  • Global Variables

Global variables are troublesome because they do not allow names to be reused among functions. It is better to use one of the other two methods.

For example, suppose you want to minimize the function

 
f(x)=(abx21+x41/3)x21+x1x2+(c+cx22)x22(2-2)

for different values of ab, and c. Solvers accept objective functions that depend only on a single variable (x in this case). The following sections show how to provide the additional parameters ab, and c. The solutions are for parameter values a = 4, b = 2.1, and c = 4 near x0 = [0.5 0.5] using fminunc.

 

Anonymous Functions

To pass parameters using anonymous functions:

  1. Write a file containing the following code:

    function y = parameterfun(x,a,b,c)
    y = (a - b*x(1)^2 + x(1)^4/3)*x(1)^2 + x(1)*x(2) + ...
        (-c + c*x(2)^2)*x(2)^2;
  2. Assign values to the parameters and define a function handle f to an anonymous function by entering the following commands at the MATLAB® prompt:

    a = 4; b = 2.1; c = 4; % Assign parameter values
    x0 = [0.5,0.5];
    f = @(x)parameterfun(x,a,b,c);
  3. Call the solver fminunc with the anonymous function:

    [x,fval] = fminunc(f,x0)

    The following output is displayed in the command window:

    Local minimum found.
    
    Optimization completed because the size of the gradient is less than
    the default value of the function tolerance.
    
    x =
       -0.0898    0.7127
    
    fval =
       -1.0316
 

Note:   The parameters passed in the anonymous function are those that exist at the time the anonymous function is created. Consider the example

a = 4; b = 2.1; c = 4;
f = @(x)parameterfun(x,a,b,c)

Suppose you subsequently change, a to 3 and run

[x,fval] = fminunc(f,x0)

You get the same answer as before, since parameterfun uses a = 4, the value when f was created.

To change the parameters that are passed to the function, renew the anonymous function by reentering it:

a = 3;
f = @(x)parameterfun(x,a,b,c)

You can create anonymous functions of more than one argument. For example, to use lsqcurvefit, first create a function that takes two input arguments, x and xdata:

fh = @(x,xdata)(sin(x).*xdata +(x.^2).*cos(xdata));
x = pi; xdata = pi*[4;2;3];
fh(x, xdata)

ans =

    9.8696
    9.8696
   -9.8696

Now call lsqcurvefit:

% Assume ydata exists
x = lsqcurvefit(fh,x,xdata,ydata)
 

Nested Functions

To pass the parameters for Equation 2-2 via a nested function, write a single file that

  • Accepts abc, and x0 as inputs

  • Contains the objective function as a nested function

  • Calls fminunc

Here is the code for the function file for this example:

function [x,fval] =  runnested(a,b,c,x0) 
[x,fval] = fminunc(@nestedfun,x0);
% Nested function that computes the objective function     
    function y = nestedfun(x)
        y = (a - b*x(1)^2 + x(1)^4/3)*x(1)^2 + x(1)*x(2) +...
            (-c + c*x(2)^2)*x(2)^2;     
    end
end

The objective function is the nested function nestedfun, which has access to the variables ab, and c.

To run the optimization, enter:

a = 4; b = 2.1; c = 4;% Assign parameter values
x0 = [0.5,0.5];
[x,fval] = runnested(a,b,c,x0)

The output is the same as in Anonymous Functions.

 

Global Variables

Global variables can be troublesome, so it is better to avoid using them. To use global variables, declare the variables to be global in the workspace and in the functions that use the variables.

  1. Write a function file:

    function y = globalfun(x)
    global a b c
    y = (a - b*x(1)^2 + x(1)^4/3)*x(1)^2 + x(1)*x(2) + ...
        (-c + c*x(2)^2)*x(2)^2;
  2. In your MATLAB workspace, define the variables and run fminunc:

    global a b c;
    a = 4; b = 2.1; c = 4; % Assign parameter values
    x0 = [0.5,0.5];
    [x,fval] = fminunc(@globalfun,x0)

The output is the same as in Anonymous Functions.

参考资料:matlab帮助文档

转载于:https://www.cnblogs.com/Qiangcm/p/8794247.html

相关文章:

  • CQOI2018 游记 再见OI,既是反思,也是祝福
  • CPU的系统总线
  • OpenCV/OpenCL/OpenGL区别
  • 工厂方法模式
  • Spring Cloud学习笔记-007
  • Reverse Integer
  • W650DC_DD_CFL_Win64(刷八代CPUBios)
  • Resource 的 IsSealed 问题
  • Machine Learning 第三周
  • React Native报错undefined is not an object(evaluating ‘_reactnative.propTypes’)解决办法...
  • Vue.js 子组件的异步加载及其生命周期控制
  • BZOJ 1283 序列 费用流 网络流 线性规划
  • kafka知识体系-kafka leader选举
  • 数据概述
  • winform控件大全
  • [译]Python中的类属性与实例属性的区别
  • 《剑指offer》分解让复杂问题更简单
  • 【跃迁之路】【463天】刻意练习系列222(2018.05.14)
  • android百种动画侧滑库、步骤视图、TextView效果、社交、搜房、K线图等源码
  • Angular js 常用指令ng-if、ng-class、ng-option、ng-value、ng-click是如何使用的?
  • javascript面向对象之创建对象
  • javascript数组去重/查找/插入/删除
  • JS创建对象模式及其对象原型链探究(一):Object模式
  • js正则,这点儿就够用了
  • LeetCode算法系列_0891_子序列宽度之和
  • 分布式熔断降级平台aegis
  • 给初学者:JavaScript 中数组操作注意点
  • 紧急通知:《观止-微软》请在经管柜购买!
  • 浅谈Kotlin实战篇之自定义View图片圆角简单应用(一)
  • 王永庆:技术创新改变教育未来
  • 新书推荐|Windows黑客编程技术详解
  • 一文看透浏览器架构
  • 用jquery写贪吃蛇
  • 做一名精致的JavaScripter 01:JavaScript简介
  • ​DB-Engines 11月数据库排名:PostgreSQL坐稳同期涨幅榜冠军宝座
  • # 20155222 2016-2017-2 《Java程序设计》第5周学习总结
  • #Linux(权限管理)
  • #我与Java虚拟机的故事#连载05:Java虚拟机的修炼之道
  • %3cli%3e连接html页面,html+canvas实现屏幕截取
  • ( 用例图)定义了系统的功能需求,它是从系统的外部看系统功能,并不描述系统内部对功能的具体实现
  • (32位汇编 五)mov/add/sub/and/or/xor/not
  • (层次遍历)104. 二叉树的最大深度
  • (仿QQ聊天消息列表加载)wp7 listbox 列表项逐一加载的一种实现方式,以及加入渐显动画...
  • (六)什么是Vite——热更新时vite、webpack做了什么
  • (十七)devops持续集成开发——使用jenkins流水线pipeline方式发布一个微服务项目
  • (使用vite搭建vue3项目(vite + vue3 + vue router + pinia + element plus))
  • (新)网络工程师考点串讲与真题详解
  • (转)拼包函数及网络封包的异常处理(含代码)
  • .Net core 6.0 升8.0
  • .NET NPOI导出Excel详解
  • .NET 将多个程序集合并成单一程序集的 4+3 种方法
  • .Net 路由处理厉害了
  • .NET 命令行参数包含应用程序路径吗?
  • .net 写了一个支持重试、熔断和超时策略的 HttpClient 实例池
  • .NET 应用启用与禁用自动生成绑定重定向 (bindingRedirect),解决不同版本 dll 的依赖问题