以下代码的vs2019工程文件打包点此下载去掉运行部分的注释即可

/*powell.cpp*/
#include "stdafx.h"
#include <stdio.h>
#include <math.h>
#include "Powell.h"
#include <iostream>
#define m 10              /*数组长度m  >=  维数n    */float powell::powell_f(float x[])
{float result;//result = (x[0] - 2)*(x[0] - 2) + (x[1] - 3)*(x[1] - 3) + (x[2] - 4)*(x[2] - 4);result = x[0] * x[0] + 2 * x[1] * x[1] - 2 * x[0] * x[1] - 4 * x[0];return result;
}/*多维进退法子程序*/
void powell::mjtf(int n, float x0[], float h, float s[], float a[], float b[])
{int i;float x1[m], x2[m], x3[m], powell_f1, powell_f2, powell_f3;for (i = 0; i<n; i++)         /*计算初始两试点*/{x1[i] = x0[i];x2[i] = x0[i] + h*s[i];}powell_f1 = powell_f(x1);powell_f2 = powell_f(x2);if (powell_f2 >= powell_f1)               /*判断搜索方向*/{                       /*搜索方向为反向,转身*/h = (-1)*h;for (i = 0; i<n; i++)x3[i] = x1[i];powell_f3 = powell_f1;for (i = 0; i<n; i++)x1[i] = x2[i];powell_f1 = powell_f2;for (i = 0; i<n; i++)x2[i] = x3[i];powell_f2 = powell_f3;}                       /*搜索方向为正向*/for (i = 0; i<n; i++)         /*计算第三试点*/x3[i] = x2[i] + h*s[i];powell_f3 = powell_f(x3);while (powell_f3<powell_f2)             /*判断是否未完成搜索*/{                       /*未完成,继续搜索*/h = 2 * h;for (i = 0; i<n; i++)x1[i] = x2[i];powell_f1 = powell_f2;for (i = 0; i<n; i++)x2[i] = x3[i];powell_f2 = powell_f3;for (i = 0; i<n; i++)x3[i] = x2[i] + h*s[i];powell_f3 = powell_f(x3);}                       /*已完成*/for (i = 0; i<n; i++)         /*输出初始搜索区间*/{if (x1[i]<x3[i]){a[i] = x1[i];b[i] = x3[i];}else{a[i] = x3[i];b[i] = x1[i];}}
}/*多维黄金分割法子程序*/
void powell::mhjfgf(int n, float a[], float b[], float flag, float x[])
{int i;float x1[m], x2[m], powell_f1, powell_f2, sum;for (i = 0; i<n; i++)              /*计算初始两试点*/x1[i] = b[i] - (float)0.618*(b[i] - a[i]);powell_f1 = powell_f(x1);for (i = 0; i<n; i++)x2[i] = a[i] + (float)0.618*(b[i] - a[i]);powell_f2 = powell_f(x2);do{if (powell_f1 <= powell_f2)                  /*判断消去区间*/{                          /*消去右*/for (i = 0; i<n; i++)b[i] = x2[i];for (i = 0; i<n; i++)x2[i] = x1[i];powell_f2 = powell_f1;for (i = 0; i<n; i++)x1[i] = b[i] - (float)0.618*(b[i] - a[i]);powell_f1 = powell_f(x1);}else{                          /*消去左*/for (i = 0; i<n; i++)a[i] = x1[i];for (i = 0; i<n; i++)x1[i] = x2[i];powell_f1 = powell_f2;for (i = 0; i<n; i++)x2[i] = a[i] + (float)0.618*(b[i] - a[i]);powell_f2 = powell_f(x2);}sum = 0;for (i = 0; i<n; i++)sum += (b[i] - a[i])*(b[i] - a[i]);} while (sqrt(sum)>flag*0.1);for (i = 0; i<n; i++)x[i] = (float)0.5*(b[i] + a[i]);
}/*鲍威尔法子程序*/
void powell::mbwef(int n, float x0[], float h, float flag, float a[], float b[], float x[])
{int i, j, k, r;float x1[m], x11[m], x2[m], powell_f0, powell_f1, powell_f2, fn[m], s[m][m], sum;for (i = 0; i<n; i++)//构造一个单位对角矩阵sfor (k = 0; k<n; k++)if (i == k)s[i][k] = 1;elses[i][k] = 0;k = 0;while (1){for (i = 0; i<n; i++){x1[i] = x0[i];x11[i] = x1[i];}for (i = 0; i<n; i++){mjtf(n, x1, h, s[i], a, b);mhjfgf(n, a, b, flag, x1);fn[i] = powell_f(x11) - powell_f(x1);}for (i = 0; i<n; i++)x2[i] = 2 * x1[i] - x0[i];for (i = 1; i<n; i++)if (fn[0]<fn[i]){fn[0] = fn[i];r = i;}elser = 0;powell_f0 = powell_f(x0);powell_f1 = powell_f(x1);powell_f2 = powell_f(x2);if (powell_f2 >= powell_f0 || (powell_f0 - 2 * powell_f1 + powell_f2)*(powell_f0 - powell_f1 - fn[0])*(powell_f0 - powell_f1 - fn[0]) >= 0.5*fn[0] * (powell_f0 - powell_f2)*(powell_f0 - powell_f2)){sum = 0;for (i = 0; i<n; i++)sum += (x1[i] - x0[i])*(x1[i] - x0[i]);if (powell_f1 <= powell_f2)for (i = 0; i<n; i++)x0[i] = x1[i];elsefor (i = 0; i<n; i++)x0[i] = x2[i];}else{for (i = r; i<n - 1; i++)for (j = 0; j<n; j++)s[i][j] = s[i + 1][j];for (i = 0; i<n; i++)s[n - 1][i] = x1[i] - x0[i];mjtf(n, x1, h, s[n - 1], a, b);mhjfgf(n, a, b, flag, x1);sum = 0;for (i = 0; i<n; i++)sum += (x1[i] - x0[i])*(x1[i] - x0[i]);for (i = 0; i<n; i++)x0[i] = x1[i];}if (sqrt(sum) <= flag)break;elsek += 1;}for (i = 0; i<n; i++)x[i] = x1[i];
}/*鲍威尔法主程序*/int main(void)
{int i, n = 2;float h, flag, x0[m], a[m], b[m], x[m];printf("\n<鲍威尔法>\n");/*printf("请输入维数:\n");scanf_s("%d", &n);*/printf("请输入初始点:");for (i = 0; i<n; i++){printf("\nx0[%d]=", i);scanf_s("%f", &x0[i]);}printf("\n请输入初始步长:\n");scanf_s("%f", &h);printf("\n请输入精度:\n");scanf_s("%f", &flag);powell one;one.mbwef(n, x0, h, flag, a, b, x);printf("\n极小点坐标为:\n");for (i = 0; i<n; i++)printf("x[%d]=%f\n", i, x[i]);printf("\n极小值为:\n%f\n", one.powell_f(x));while (1){;}}
#ifndef _powell_h_
#define _powell_h_class powell
{public:float powell_f(float x[]);void mjtf(int n, float x0[], float h, float s[], float a[], float b[]);void mhjfgf(int n, float a[], float b[], float flag, float x[]);void mbwef(int n, float x0[], float h, float flag, float a[], float b[], float x[]);
private:};#endif // !
#pragma once

powell法c/c++程序相关推荐

  1. 鲍威尔方法c语言程序,鲍威尔法编程-powell法编程-c语言编程-c++6.0.doc

    鲍威尔法编程-powell法编程-c语言编程-c++6.0.doc include stdio.hdefine N 2float gsfloat zNfloat f;f10*z0z1-5*z0z1-5 ...

  2. 用POWELL法求极小值:Rosenbrock函数

    函数: 一.题目分析: 在数学最优化中,Rosenbrock 函数是一个用来测试最优化算法性能的非凸函数,由 Howard Harry Rosenbrock 在 1960 年提出 .也称为 Rosen ...

  3. 漫步最优化四十——Powell法(上)

    平凡无所谓,\textbf{平凡无所谓,} 平淡无所谓,\textbf{平淡无所谓,} 但求每天早晨能望见你,\textbf{但求每天早晨能望见你,} 这已经足够满足.\textbf{这已经足够满足. ...

  4. 31套VTK3D图像体绘制/VTK光线投射法/VTK三维重建程序源码

    31套VTK3D图像体绘制/VTK光线投射法/VTK三维重建程序源码 1.基于VTK的3D图像体绘制(实现了以VTK为基础的3D图像重建) 2.基于VTK的人头骨3D图像(用表面重建的方法.vs200 ...

  5. 雨流计数法python程序_雨流计数法及其在程序中的具体实现

    雨流计数法及其在程序中的具体实现 董乐义 , 罗俊 , 程礼 ( 西安空军工程大学工程学院 , 陕西 西安 710038) 摘 要 : 根据雨流计数法的规则和在实际中应用的体会 , 介绍了雨流计数法在 ...

  6. 软件工程习题,耦合性的概念如何与软件可移植性相关联?举例支持你的论述,应用逐步求精法为下列程序开发三种不同级别的过程抽象

    软件工程习题 11.9.耦合性的概念如何与软件可移植性相关联?举例支持你的论述 11.10.应用逐步求精法为下列程序开发三种不同级别的过程抽象,开发一个支票打印程序,给出输出金额,并按支票常规要求给出 ...

  7. MATLAB牛拉法,MATLAB潮流程序(IEEE14直角坐标牛拉法).doc

    MATLAB潮流程序(IEEE14直角坐标牛拉法) MATLAB潮流程序(IEEE14 直角坐标 牛拉法) clear baseMVA=100; %功率基值 %%读Data1中数据 load Data ...

  8. matlab使用sym类型,subs和diff函数,进行模式搜索法和改进的powell法求解函数

    如果想以分数输出结果,可以用   format rat f函数使用以前的稍加改造 function y=f(x) if(length(x)==1)global xk;global pk;x=xk+x* ...

  9. 鲍威尔c 语言程序,鲍威尔法编程-powell法编程 c语言编程 c++6.0

    fafu js 优化 #include #define N 2 float gs(float z[N]) { float f; //f=10*(z[0]+z[1]-5)*(z[0]+z[1]-5)+( ...

最新文章

  1. pandas以前笔记
  2. session和cookie的应用场景和区别
  3. WPF新手实践7:MVVM Light Toolkit(七、Messenger)
  4. 从进程说起:容器到底是怎么一回事儿?
  5. 定时任务scheduleAtFixedRate设定每天某个时刻执行
  6. 全国计算机等级考试题库二级C操作题100套(第35套)
  7. P1496 vijos1165-火烧赤壁【离散化】
  8. python函数参数传递机制_Python 学习笔记(一) 理解Python的函数传参机制
  9. 我如何开始学习Web开发
  10. android数字提示错误,从服务器接收数据时出现Android错误
  11. java manualbuffer_FlatBuffer Java Bean自由转换
  12. JDK5后的特性整理
  13. VMWare安装Ubuntu 16.04
  14. lingo纳什均衡代码_数学建模练习题.
  15. 钟平逻辑英语语法_钟平逻辑英语教程视频1-5季全(含笔记 支持百度云)
  16. 你真的熟练运用 HTML5 了吗,这10 个酷炫的 H5 特性你会几个?
  17. curry化 js_Curry的js实现
  18. O3-开源框架使用之Butterknife 8.8.1及源码浅析
  19. 《调研报告》:黑灰产哄抢消费券的两种方式
  20. 用mysql创建职工表_【典型例题】数据库——用MySQL来建立创建员工表;-Go语言中文社区...

热门文章

  1. java支付模块之支付宝
  2. AIX系统开启ftp服务
  3. level2买股技巧_同花顺Level-2教你看清个股真实交易
  4. Docker 进阶之镜像分层详解
  5. 计算机组成原理试卷分析,计算机组成原理试卷分析.doc
  6. 前端学习笔记36-水平方向的布局
  7. Excel表数据的批量导入
  8. android 网易云音乐上滑动画,Android 仿网易云音乐 音轨跳动效果
  9. NMS(非极大值抑制)的python,cpu,gpu实现
  10. SQLyog导出csv文件内容都在一列的解决办法