首页 > 其他分享 >5.18

5.18

时间:2023-06-11 09:04:40浏览次数:29  
标签:opt 迭代 fprintf iterations 5.18 grad x0

% 定义目标函数
f = @(x) 100*(x(1)^2 - x(2))^2 + (x(1) - 1)^2;

% 定义目标函数的梯度
grad_f = @(x) [400*x(1)*(x(1)^2 - x(2)) + 2*(x(1) - 1); -200*(x(1)^2 - x(2))];

% 定义终止准则
epsilon = 1e-5;

% 定义最大迭代次数
max_iterations = 1000;

% 初始点列表
initial_points = [0, 0; -1, 1; 2, -2];

% 迭代过程函数
function [x_opt, f_opt, iterations] = gradient_descent(f, grad_f, x0, epsilon, max_iterations)
x = x0; % 初始化x
f_values = []; % 用于存储每次迭代的函数值
iterations = 0; % 迭代次数

while iterations < max_iterations
f_values = [f_values, f(x)]; % 记录当前迭代的函数值
grad = grad_f(x); % 计算梯度
alpha = 0.01; % 步长(学习率)选择为常数0.01
x = x - alpha * grad; % 更新x
iterations = iterations + 1; % 迭代次数加1

% 判断终止准则
if norm(grad) < epsilon
break;
end
end

x_opt = x; % 最优解
f_opt = f(x); % 最优函数值
end

% 使用最速下降法求解并输出结果
for i = 1:size(initial_points, 1)
x0 = initial_points(i, :);
[x_opt, f_opt, iterations] = gradient_descent(f, grad_f, x0, epsilon, max_iterations);
fprintf('初始点: [%d, %d]\n', x0);
fprintf('迭代次数: %d\n', iterations);
fprintf('最优函数值: %.6f\n', f_opt);
fprintf('最优解: [%.6f, %.6f]\n', x_opt);
fprintf('-------------------------------------\n');
end

标签:opt,迭代,fprintf,iterations,5.18,grad,x0
From: https://www.cnblogs.com/ruipengli/p/17472457.html

相关文章

  • 5.18
    1#include<iostream>2#include<fstream>3#include<string>45intmain(){6std::ifstreaminputFile("D://article.txt");78if(!inputFile){9std::cout<<"无法打开输入文件!"<<......
  • 5.18
    #include<iostream>usingnamespacestd;intmain(){      doublea;      double*p=&a;      cout<<"指针占了"<<sizeof(double)<<"字节"<<endl;      cout<<"指针所指向的变量占了"<<sizeof(p)&......
  • 编程一小时2023.5.18
    #include<iostream>#include<fstream>usingnamespacestd;classDog{public:intgetdogage(){returnage;}intgetdogweight(){returnweight;}voidsetdog(intx,inty){age=x;weight=y;}private:intage,weight;}; intmain(){intv1,v2;......
  • 5.18打卡
    一、实验内容定义一个Dog类,包括体重和年龄两个数据成员及其成员函数,声明一个实例dog1,体重5,年龄10,使用I/O流把dog1的状态写入磁盘文件。再声明一个实例dog2,通过读取文件dog1的状态赋给dog2。分别用文本方式和二进制方式操作文件。二、实验代码#include<bits/stdc++.h>usingna......
  • 5.18每日总结
    今日进行了python的学习。对于昨天的测试代码进行了分析学习。R7-1字典合并d1=eval(input())d2=eval(input())forkeyind2.keys():d1[key]=d1.get(key,0)+d2[key]t=list(d1.items())t.sort(key=lambdax:ord(x[0])iftype(x[0])==strelsex[0])......
  • 每日总结-23.5.18
    <%@pagelanguage="java"contentType="text/html;charset=UTF-8"pageEncoding="UTF-8"%><!DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""http://www.w3.org/TR/html4/loose.dtd&qu......
  • 5.18
    #include<iostream>usingnamespacestd;#include<string>classstudent{public:   voidshangke();protected:   stringname;   intbj;   intid;};classteacher{public:   voidjiaoke();protected:   intID;   intgz;};c......
  • 5.18总结
    packagecom.mf.jdbc.exmaple;importcom.alibaba.druid.pool.DruidDataSourceFactory;importcom.mf.jdbc.Brand;importorg.junit.Test;importjavax.sql.DataSource;importjava.io.FileInputStream;importjava.sql.Connection;importjava.sql.PreparedStatement;......
  • 5.18CSDN贪吃蛇
    贪吃蛇 速度不要调很慢会影响判断#include<iostream>#include<windows.h>#include<conio.h>#include<deque>#include<ctime>#include<stdexcept>usingnamespacestd;structSnake{//蛇类结构体charimage;shortx,y;//坐标};classsnakeGame......
  • 2023.5.18
    importosimportpandasaspd#添加测试数据os.makedirs(os.path.join('.','data'),exist_ok=True)data_file=os.path.join('.','data','house_tiny.csv')withopen(data_file,'w')asf:   f.write('N......