首页
学习
活动
专区
圈层
工具
发布

MATLAB大作业选登与DeepSeek-R1实现-单纯形法求解线性规划

[编者注:]这个题目的实现比较简单,只有一个按钮的回调函数,所以,DeepSeek-R1给的是步骤,不是可以傻瓜式运行的程序,但为了完成DeepSeek-R1实现结果,编者必须进行集成,一步一步实现,所以,还是花了一定时间,力求步骤详细。本程序运行需要Optimization Toolbox工具箱。

一、软件介绍

先从软件帮助文件中的使用方法来看一下软件功能:'本软件提供的单纯形法只提供最小值的求解和小于等于的约束条件形式';'请您通过取负号的方式使您需要求解的问题满足该形式';'所有的数值输入请用英文字符并以矩阵的形式输入';'c中输入目标函数;';'a,b中输入约束线性条件;';'aeq,beq中输入非线性约束条件;';'请保证您在对单纯形法有一定了解的基础上使用该软件'。

二、软件运行情况

1、程序启动后的界面(编者为大作业添加了初始化输入项)

2、点击计算按钮后,结果显示在文本窗口

3、只有线性约束条件的情况下,计算结果(为了与DeepSeek-R1给的计算结果比较,同样的约束条件下,两者结果一致)

三、大作业设计视图

四、大作业代码视图

classdef app1 < matlab.apps.AppBase

% Properties that correspond to app components

properties (Access = public)

      UIFigure             matlab.ui.Figure

      Menu                 matlab.ui.container.Menu

      Menu_2               matlab.ui.container.Menu

      beqEditField         matlab.ui.control.EditField

      beqEditFieldLabel    matlab.ui.control.Label

      aeqEditField         matlab.ui.control.EditField

      aeqEditFieldLabel    matlab.ui.control.Label

      bEditField           matlab.ui.control.EditField

      bEditFieldLabel      matlab.ui.control.Label

      aEditField           matlab.ui.control.EditField

      aEditFieldLabel      matlab.ui.control.Label

      cEditField           matlab.ui.control.EditField

      cEditFieldLabel      matlab.ui.control.Label

      solveButton          matlab.ui.control.Button

      resultTextArea       matlab.ui.control.TextArea

      resultTextAreaLabel  matlab.ui.control.Label

end

% Callbacks that handle component events

methods (Access = private)

% Button pushed function: solveButton

function solveButtonPushed(app, event)

% Button pushed function: solveButton

%function solveButtonPushed(app, event)

          c = str2num(app.cEditField.Value);

          A = str2num(app.aEditField.Value);

          b = str2num(app.bEditField.Value);

          Aeq = str2num(app.aeqEditField.Value);

          beq = str2num(app.beqEditField.Value);

% 如果用户未输入等式约束,则设置为空矩阵

if isempty(Aeq)

              Aeq = [];

              beq = [];

end

% 设置linprog选项为单纯形法

          options = optimoptions('linprog','Algorithm','dual-simplex');

% 运行linprog

          [x,fval,exitflag,~] = linprog(-c,A,b,Aeq,beq,[],[],options);

% 判断并处理linprog的输出结果

if exitflag == 1

              app.resultTextArea.Value = ['Optimal solution found: x = [' num2str(x') '], fval = ' num2str(-fval)];

else

              app.resultTextArea.Value = ['Solver could not find an optimal solution. Exit Flag: ' num2str(exitflag)];

end

%end

end

% Value changed function: aEditField

function aEditFieldValueChanged(app, event)

end

% Menu selected function: Menu_2

function Menu_2Selected(app, event)

% 创建菜单页

          menuPage = uifigure('Name', '操作帮助');

% 创建uitable控件,并设置其Position属性和列宽度

          table = uitable(menuPage, 'Position', [50 50 500 250]);

          table.ColumnWidth = {500};

% 创建输出内容,使用cell数组存储每一行文本

          content = {'本软件的使用方法:';

'本软件提供的单纯形法只提供最小值的求解和小于等于的约束条件形式';

'请您通过取负号的方式使您需要求解的问题满足该形式';

'所有的数值输入请用英文字符并以矩阵的形式输入';

'c中输入目标函数;';

'a,b中输入约束线性条件;';

'aeq,beq中输入非线性约束条件;';

'请保证您在对单纯形法有一定了解的基础上使用该软件'};

% 将内容设置到uitable控件的Data属性,实现向左对齐

          table.Data = content;

end

% Menu selected function: Menu

function MenuSelected(app, event)

%menuPage=uifigure('Name','');

%table=uitable(menuPage,'Position',[50 50 500 250]);

%content={'start'};

end

end

% Component initialization

methods (Access = private)

% Create UIFigure and components

function createComponents(app)

% Create UIFigure and hide until all components are created

          app.UIFigure = uifigure('Visible', 'off');

          app.UIFigure.Position = [100 100 640 480];

          app.UIFigure.Name = 'MATLAB App';

% Create Menu

          app.Menu = uimenu(app.UIFigure);

          app.Menu.MenuSelectedFcn = createCallbackFcn(app, @MenuSelected, true);

          app.Menu.Text = '开始';

% Create Menu_2

          app.Menu_2 = uimenu(app.UIFigure);

          app.Menu_2.MenuSelectedFcn = createCallbackFcn(app, @Menu_2Selected, true);

          app.Menu_2.Text = '操作帮助';

% Create resultTextAreaLabel

          app.resultTextAreaLabel = uilabel(app.UIFigure);

          app.resultTextAreaLabel.HorizontalAlignment = 'right';

          app.resultTextAreaLabel.Position = [18 187 82 29];

          app.resultTextAreaLabel.Text = {'resultTextArea'; '计算结果'};

% Create resultTextArea

          app.resultTextArea = uitextarea(app.UIFigure);

          app.resultTextArea.Position = [138 13 434 205];

% Create solveButton

          app.solveButton = uibutton(app.UIFigure, 'push');

          app.solveButton.ButtonPushedFcn = createCallbackFcn(app, @solveButtonPushed, true);

          app.solveButton.Position = [468 243 104 85];

          app.solveButton.Text = '点击计算';

% Create cEditFieldLabel

          app.cEditFieldLabel = uilabel(app.UIFigure);

          app.cEditFieldLabel.HorizontalAlignment = 'right';

          app.cEditFieldLabel.Position = [41 385 58 44];

          app.cEditFieldLabel.Text = {'cEditField'; '在此输入'; '目标函数'};

% Create cEditField

          app.cEditField = uieditfield(app.UIFigure, 'text');

          app.cEditField.Position = [114 378 85 80];

          app.cEditField.Value = '[ -3, -2 ]';

% Create aEditFieldLabel

          app.aEditFieldLabel = uilabel(app.UIFigure);

          app.aEditFieldLabel.HorizontalAlignment = 'right';

          app.aEditFieldLabel.Position = [222 385 58 44];

          app.aEditFieldLabel.Text = {'aEditField'; '在此输入'; '约束条件'};

% Create aEditField

          app.aEditField = uieditfield(app.UIFigure, 'text');

          app.aEditField.ValueChangedFcn = createCallbackFcn(app, @aEditFieldValueChanged, true);

          app.aEditField.Position = [295 378 85 80];

          app.aEditField.Value = '[2 1;1 1;1 0]';

% Create bEditFieldLabel

          app.bEditFieldLabel = uilabel(app.UIFigure);

          app.bEditFieldLabel.HorizontalAlignment = 'right';

          app.bEditFieldLabel.Position = [414 385 58 44];

          app.bEditFieldLabel.Text = {'bEditField'; '在此输入'; '系数'};

% Create bEditField

          app.bEditField = uieditfield(app.UIFigure, 'text');

          app.bEditField.Position = [487 378 85 80];

          app.bEditField.Value = '[18; 15; 7]';

% Create aeqEditFieldLabel

          app.aeqEditFieldLabel = uilabel(app.UIFigure);

          app.aeqEditFieldLabel.HorizontalAlignment = 'right';

          app.aeqEditFieldLabel.Position = [27 235 72 59];

          app.aeqEditFieldLabel.Text = {'aeqEditField'; '在此输入'; '非线性'; '约束条件'};

% Create aeqEditField

          app.aeqEditField = uieditfield(app.UIFigure, 'text');

          app.aeqEditField.Position = [114 243 85 80];

          app.aeqEditField.Value = '[1 2;3 4]';

% Create beqEditFieldLabel

          app.beqEditFieldLabel = uilabel(app.UIFigure);

          app.beqEditFieldLabel.HorizontalAlignment = 'right';

          app.beqEditFieldLabel.Position = [209 235 72 59];

          app.beqEditFieldLabel.Text = {'beqEditField'; '在此输入'; '非线性'; '系数'};

% Create beqEditField

          app.beqEditField = uieditfield(app.UIFigure, 'text');

          app.beqEditField.Position = [296 243 85 80];

          app.beqEditField.Value = '[5;6]';

% Show the figure after all components are created

          app.UIFigure.Visible = 'on';

end

end

% App creation and deletion

methods (Access = public)

% Construct app

function app = app1

% Create UIFigure and components

          createComponents(app)

% Register the app with App Designer

          registerApp(app, app.UIFigure)

if nargout == 0

              clear app

end

end

% Code that executes before app deletion

function delete(app)

% Delete UIFigure when app is deleted

          delete(app.UIFigure)

end

end

end

五、代码形式大作业程序

.rtcContent { padding: 30px; } .lineNode {font-size: 10pt; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-style: normal; font-weight: normal; }classdef app1 < matlab.apps.AppBase   % Properties that correspond to app components   properties (Access = public)       UIFigure             matlab.ui.Figure       Menu                 matlab.ui.container.Menu       Menu_2               matlab.ui.container.Menu       beqEditField         matlab.ui.control.EditField       beqEditFieldLabel    matlab.ui.control.Label       aeqEditField         matlab.ui.control.EditField       aeqEditFieldLabel    matlab.ui.control.Label       bEditField           matlab.ui.control.EditField       bEditFieldLabel      matlab.ui.control.Label       aEditField           matlab.ui.control.EditField       aEditFieldLabel      matlab.ui.control.Label       cEditField           matlab.ui.control.EditField       cEditFieldLabel      matlab.ui.control.Label       solveButton          matlab.ui.control.Button       resultTextArea       matlab.ui.control.TextArea       resultTextAreaLabel  matlab.ui.control.Label   end   % Callbacks that handle component events   methods (Access = private)       % Button pushed function: solveButton       function solveButtonPushed(app, event)           % Button pushed function: solveButton           %function solveButtonPushed(app, event)           c = str2num(app.cEditField.Value);           A = str2num(app.aEditField.Value);           b = str2num(app.bEditField.Value);           Aeq = str2num(app.aeqEditField.Value);           beq = str2num(app.beqEditField.Value);           % 如果用户未输入等式约束,则设置为空矩阵           if isempty(Aeq)               Aeq = [];               beq = [];           end           % 设置linprog选项为单纯形法           options = optimoptions('linprog','Algorithm','dual-simplex');           % 运行linprog           [x,fval,exitflag,~] = linprog(-c,A,b,Aeq,beq,[],[],options);           % 判断并处理linprog的输出结果           if exitflag == 1               app.resultTextArea.Value = ['Optimal solution found: x = [' num2str(x') '], fval = ' num2str(-fval)];           else               app.resultTextArea.Value = ['Solver could not find an optimal solution. Exit Flag: ' num2str(exitflag)];           end           %end       end       % Value changed function: aEditField       function aEditFieldValueChanged(app, event)       end       % Menu selected function: Menu_2       function Menu_2Selected(app, event)           % 创建菜单页           menuPage = uifigure('Name', '操作帮助');           % 创建uitable控件,并设置其Position属性和列宽度           table = uitable(menuPage, 'Position', [50 50 500 250]);           table.ColumnWidth = {500};           % 创建输出内容,使用cell数组存储每一行文本           content = {'本软件的使用方法:';               '本软件提供的单纯形法只提供最小值的求解和小于等于的约束条件形式';               '请您通过取负号的方式使您需要求解的问题满足该形式';               '所有的数值输入请用英文字符并以矩阵的形式输入';               'c中输入目标函数;';               'a,b中输入约束线性条件;';               'aeq,beq中输入非线性约束条件;';               '请保证您在对单纯形法有一定了解的基础上使用该软件'};           % 将内容设置到uitable控件的Data属性,实现向左对齐           table.Data = content;       end       % Menu selected function: Menu       function MenuSelected(app, event)           %menuPage=uifigure('Name','');           %table=uitable(menuPage,'Position',[50 50 500 250]);           %content={'start'};       end   end   % Component initialization   methods (Access = private)       % Create UIFigure and components       function createComponents(app)           % Create UIFigure and hide until all components are created           app.UIFigure = uifigure('Visible', 'off');           app.UIFigure.Position = [100 100 640 480];           app.UIFigure.Name = 'MATLAB App';           % Create Menu           app.Menu = uimenu(app.UIFigure);           app.Menu.MenuSelectedFcn = createCallbackFcn(app, @MenuSelected, true);           app.Menu.Text = '开始';           % Create Menu_2           app.Menu_2 = uimenu(app.UIFigure);           app.Menu_2.MenuSelectedFcn = createCallbackFcn(app, @Menu_2Selected, true);           app.Menu_2.Text = '操作帮助';           % Create resultTextAreaLabel           app.resultTextAreaLabel = uilabel(app.UIFigure);           app.resultTextAreaLabel.HorizontalAlignment = 'right';           app.resultTextAreaLabel.Position = [18 187 82 29];           app.resultTextAreaLabel.Text = {'resultTextArea'; '计算结果'};           % Create resultTextArea           app.resultTextArea = uitextarea(app.UIFigure);           app.resultTextArea.Position = [138 13 434 205];           % Create solveButton           app.solveButton = uibutton(app.UIFigure, 'push');           app.solveButton.ButtonPushedFcn = createCallbackFcn(app, @solveButtonPushed, true);           app.solveButton.Position = [468 243 104 85];           app.solveButton.Text = '点击计算';           % Create cEditFieldLabel           app.cEditFieldLabel = uilabel(app.UIFigure);           app.cEditFieldLabel.HorizontalAlignment = 'right';           app.cEditFieldLabel.Position = [41 385 58 44];           app.cEditFieldLabel.Text = {'cEditField'; '在此输入'; '目标函数'};           % Create cEditField           app.cEditField = uieditfield(app.UIFigure, 'text');           app.cEditField.Position = [114 378 85 80];           app.cEditField.Value = '[ -3, -2 ]';           % Create aEditFieldLabel           app.aEditFieldLabel = uilabel(app.UIFigure);           app.aEditFieldLabel.HorizontalAlignment = 'right';           app.aEditFieldLabel.Position = [222 385 58 44];           app.aEditFieldLabel.Text = {'aEditField'; '在此输入'; '约束条件'};           % Create aEditField           app.aEditField = uieditfield(app.UIFigure, 'text');           app.aEditField.ValueChangedFcn = createCallbackFcn(app, @aEditFieldValueChanged, true);           app.aEditField.Position = [295 378 85 80];           app.aEditField.Value = '[2 1;1 1;1 0]';           % Create bEditFieldLabel           app.bEditFieldLabel = uilabel(app.UIFigure);           app.bEditFieldLabel.HorizontalAlignment = 'right';           app.bEditFieldLabel.Position = [414 385 58 44];           app.bEditFieldLabel.Text = {'bEditField'; '在此输入'; '系数'};           % Create bEditField           app.bEditField = uieditfield(app.UIFigure, 'text');           app.bEditField.Position = [487 378 85 80];           app.bEditField.Value = '[18; 15; 7]';           % Create aeqEditFieldLabel           app.aeqEditFieldLabel = uilabel(app.UIFigure);           app.aeqEditFieldLabel.HorizontalAlignment = 'right';           app.aeqEditFieldLabel.Position = [27 235 72 59];           app.aeqEditFieldLabel.Text = {'aeqEditField'; '在此输入'; '非线性'; '约束条件'};           % Create aeqEditField           app.aeqEditField = uieditfield(app.UIFigure, 'text');           app.aeqEditField.Position = [114 243 85 80];           app.aeqEditField.Value = '[1 2;3 4]';           % Create beqEditFieldLabel           app.beqEditFieldLabel = uilabel(app.UIFigure);           app.beqEditFieldLabel.HorizontalAlignment = 'right';           app.beqEditFieldLabel.Position = [209 235 72 59];           app.beqEditFieldLabel.Text = {'beqEditField'; '在此输入'; '非线性'; '系数'};           % Create beqEditField           app.beqEditField = uieditfield(app.UIFigure, 'text');           app.beqEditField.Position = [296 243 85 80];           app.beqEditField.Value = '[5;6]';           % Show the figure after all components are created           app.UIFigure.Visible = 'on';       end   end   % App creation and deletion   methods (Access = public)       % Construct app       function app = app1           % Create UIFigure and components           createComponents(app)           % Register the app with App Designer           registerApp(app, app.UIFigure)           if nargout == 0               clear app           end       end       % Code that executes before app deletion       function delete(app)           % Delete UIFigure when app is deleted           delete(app.UIFigure)       end   endend

六、大作业文件清单

七、DeepSeek-R1实现

1、提问

(根据大作业软件内容介绍总结为)帮我在matlab环境下使用App Designer工具写一个单纯形法求解线性规划问题的小程序,有界面,使用编辑框输入相关数据,如目标函数、线性约束条件和系数、非线性约束条件和系数等,然后点击计算按钮输出,结果显示在一个大的文本区域中,提供最小值的求解和小于等于的约束条件形式,并在编辑框中提供缺省输入信息,用户点击计算按钮即可运行。

2、思考

3、成果

4、集成

(1)DeepSeek-R1步骤1:创建App Designer项目

打开MATLAB,在命令行输入appdesigner启动设计工具,新建一个空白应用程序名称取SimplexMethod。

(2)DeepSeek-R1步骤2:设计界面布局

按以下结构添加组件:

a.文本标签和编辑框(目标函数系数)

b.文本标签和编辑框(约束矩阵A)

c.文本标签和编辑框(约束向量b)

d.计算按钮

e.结果显示文本区域

(3)DeepSeek-R1步骤3:设置组件属性

% 组件主要属性设置:

% 目标函数输入框 - edit_f

% 约束矩阵A输入框 - edit_A

% 约束向量b输入框 - edit_b

% 计算按钮 - button_calculate

% 结果文本区域 - textarea_result

% 设置默认值:

app.edit_f.Value = '[-3 -2]';

app.edit_A.Value = '[2 1; 1 1; 1 0]';

app.edit_b.Value = '[18; 15; 7]';

注:以上信息在第(2)步设计界面布局时同步添加完成

(4)DeepSeek-R1步骤4:编写计算按钮回调函数

在计算按钮上右击,指向按钮的回调函数,然后把DeepSeek-R1给的步骤4:编写计算按钮回调函数中的代码粘贴于此。

5、运行结果(与大作业结果一致)

6、程序代码视图

classdef SimplexMethod < matlab.apps.AppBase

% Properties that correspond to app components

properties (Access = public)

      UIFigure          matlab.ui.Figure

      textarea_result   matlab.ui.control.TextArea

      Label_2           matlab.ui.control.Label

      button_calculate  matlab.ui.control.Button

      edit_b            matlab.ui.control.EditField

      bEditFieldLabel   matlab.ui.control.Label

      edit_A            matlab.ui.control.EditField

      AEditFieldLabel   matlab.ui.control.Label

      edit_f            matlab.ui.control.EditField

      Label             matlab.ui.control.Label

end

% Callbacks that handle component events

methods (Access = private)

% Button pushed function: button_calculate

function button_calculateButtonPushed(app, event)

try

% 获取输入数据

              f = str2num(app.edit_f.Value);

              A = str2num(app.edit_A.Value);

              b = str2num(app.edit_b.Value);

% 输入验证

if isempty(f) || isempty(A) || isempty(b)

                  error('输入不能为空');

end

if size(A,1) ~= length(b)

                  error('A的行数与b的长度不一致');

end

if size(A,2) ~= length(f)

                  error('A的列数应与目标函数变量数一致');

end

% 求解线性规划

              lb = zeros(1,length(f));

              options = optimoptions('linprog','Display','none');

              [x, fval, exitflag] = linprog(f, A, b, [], [], lb, [], options);

% 处理结果

              result_str = '';

if exitflag == 1

                  result_str = sprintf('最优解:\n');

for i = 1:length(x)

                      result_str = sprintf('%sx%d = %.4f\n', result_str, i, x(i));

end

                  result_str = sprintf('%s目标函数最小值:%.4f', result_str, fval);

else

                  result_str = sprintf('求解失败 (退出码%d)', exitflag);

end

              app.textarea_result.Value = result_str;

catch ME

              app.textarea_result.Value = ME.message;

end

end

end

% Component initialization

methods (Access = private)

% Create UIFigure and components

function createComponents(app)

% Create UIFigure and hide until all components are created

          app.UIFigure = uifigure('Visible', 'off');

          app.UIFigure.Position = [100 100 674 476];

          app.UIFigure.Name = 'MATLAB App';

% Create Label

          app.Label = uilabel(app.UIFigure);

          app.Label.HorizontalAlignment = 'right';

          app.Label.Position = [32 383 77 22];

          app.Label.Text = '目标函数系数';

% Create edit_f

          app.edit_f = uieditfield(app.UIFigure, 'text');

          app.edit_f.Tag = 'edit_f';

          app.edit_f.Position = [124 364 82 59];

          app.edit_f.Value = '[-3 -2]';

% Create AEditFieldLabel

          app.AEditFieldLabel = uilabel(app.UIFigure);

          app.AEditFieldLabel.HorizontalAlignment = 'right';

          app.AEditFieldLabel.Position = [266 382 61 22];

          app.AEditFieldLabel.Text = '约束矩阵A';

% Create edit_A

          app.edit_A = uieditfield(app.UIFigure, 'text');

          app.edit_A.Tag = 'edit_A';

          app.edit_A.Position = [342 364 67 58];

          app.edit_A.Value = '[2 1; 1 1; 1 0]';

% Create bEditFieldLabel

          app.bEditFieldLabel = uilabel(app.UIFigure);

          app.bEditFieldLabel.HorizontalAlignment = 'right';

          app.bEditFieldLabel.Position = [464 377 60 22];

          app.bEditFieldLabel.Text = '约束向量b';

% Create edit_b

          app.edit_b = uieditfield(app.UIFigure, 'text');

          app.edit_b.Position = [539 352 82 71];

          app.edit_b.Value = '[18; 15; 7]';

% Create button_calculate

          app.button_calculate = uibutton(app.UIFigure, 'push');

          app.button_calculate.ButtonPushedFcn = createCallbackFcn(app, @button_calculateButtonPushed, true);

          app.button_calculate.Position = [33 240 135 73];

          app.button_calculate.Text = '计算按钮';

% Create Label_2

          app.Label_2 = uilabel(app.UIFigure);

          app.Label_2.HorizontalAlignment = 'right';

          app.Label_2.Position = [235 288 101 22];

          app.Label_2.Text = '结果显示文本区域';

% Create textarea_result

          app.textarea_result = uitextarea(app.UIFigure);

          app.textarea_result.Position = [351 85 278 227];

% Show the figure after all components are created

          app.UIFigure.Visible = 'on';

end

end

% App creation and deletion

methods (Access = public)

% Construct app

function app = SimplexMethod

% Create UIFigure and components

          createComponents(app)

% Register the app with App Designer

          registerApp(app, app.UIFigure)

if nargout == 0

              clear app

end

end

% Code that executes before app deletion

function delete(app)

% Delete UIFigure when app is deleted

          delete(app.UIFigure)

end

end

end

7、代码形式代码视图

.rtcContent { padding: 30px; } .lineNode {font-size: 10pt; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-style: normal; font-weight: normal; }classdef SimplexMethod < matlab.apps.AppBase   % Properties that correspond to app components   properties (Access = public)       UIFigure          matlab.ui.Figure       textarea_result   matlab.ui.control.TextArea       Label_2           matlab.ui.control.Label       button_calculate  matlab.ui.control.Button       edit_b            matlab.ui.control.EditField       bEditFieldLabel   matlab.ui.control.Label       edit_A            matlab.ui.control.EditField       AEditFieldLabel   matlab.ui.control.Label       edit_f            matlab.ui.control.EditField       Label             matlab.ui.control.Label   end   % Callbacks that handle component events   methods (Access = private)       % Button pushed function: button_calculate       function button_calculateButtonPushed(app, event)           try               % 获取输入数据               f = str2num(app.edit_f.Value);               A = str2num(app.edit_A.Value);               b = str2num(app.edit_b.Value);               % 输入验证               if isempty(f) || isempty(A) || isempty(b)                   error('输入不能为空');               end               if size(A,1) ~= length(b)                   error('A的行数与b的长度不一致');               end               if size(A,2) ~= length(f)                   error('A的列数应与目标函数变量数一致');               end               % 求解线性规划               lb = zeros(1,length(f));               options = optimoptions('linprog','Display','none');               [x, fval, exitflag] = linprog(f, A, b, [], [], lb, [], options);               % 处理结果               result_str = '';               if exitflag == 1                   result_str = sprintf('最优解:\n');                   for i = 1:length(x)                       result_str = sprintf('%sx%d = %.4f\n', result_str, i, x(i));                   end                   result_str = sprintf('%s目标函数最小值:%.4f', result_str, fval);               else                   result_str = sprintf('求解失败 (退出码%d)', exitflag);               end               app.textarea_result.Value = result_str;           catch ME               app.textarea_result.Value = ME.message;           end       end   end   % Component initialization   methods (Access = private)       % Create UIFigure and components       function createComponents(app)           % Create UIFigure and hide until all components are created           app.UIFigure = uifigure('Visible', 'off');           app.UIFigure.Position = [100 100 674 476];           app.UIFigure.Name = 'MATLAB App';           % Create Label           app.Label = uilabel(app.UIFigure);           app.Label.HorizontalAlignment = 'right';           app.Label.Position = [32 383 77 22];           app.Label.Text = '目标函数系数';           % Create edit_f           app.edit_f = uieditfield(app.UIFigure, 'text');           app.edit_f.Tag = 'edit_f';           app.edit_f.Position = [124 364 82 59];           app.edit_f.Value = '[-3 -2]';           % Create AEditFieldLabel           app.AEditFieldLabel = uilabel(app.UIFigure);           app.AEditFieldLabel.HorizontalAlignment = 'right';           app.AEditFieldLabel.Position = [266 382 61 22];           app.AEditFieldLabel.Text = '约束矩阵A';           % Create edit_A           app.edit_A = uieditfield(app.UIFigure, 'text');           app.edit_A.Tag = 'edit_A';           app.edit_A.Position = [342 364 67 58];           app.edit_A.Value = '[2 1; 1 1; 1 0]';           % Create bEditFieldLabel           app.bEditFieldLabel = uilabel(app.UIFigure);           app.bEditFieldLabel.HorizontalAlignment = 'right';           app.bEditFieldLabel.Position = [464 377 60 22];           app.bEditFieldLabel.Text = '约束向量b';           % Create edit_b           app.edit_b = uieditfield(app.UIFigure, 'text');           app.edit_b.Position = [539 352 82 71];           app.edit_b.Value = '[18; 15; 7]';           % Create button_calculate           app.button_calculate = uibutton(app.UIFigure, 'push');           app.button_calculate.ButtonPushedFcn = createCallbackFcn(app, @button_calculateButtonPushed, true);           app.button_calculate.Position = [33 240 135 73];           app.button_calculate.Text = '计算按钮';           % Create Label_2           app.Label_2 = uilabel(app.UIFigure);           app.Label_2.HorizontalAlignment = 'right';           app.Label_2.Position = [235 288 101 22];           app.Label_2.Text = '结果显示文本区域';           % Create textarea_result           app.textarea_result = uitextarea(app.UIFigure);           app.textarea_result.Position = [351 85 278 227];           % Show the figure after all components are created           app.UIFigure.Visible = 'on';       end   end   % App creation and deletion   methods (Access = public)       % Construct app       function app = SimplexMethod           % Create UIFigure and components           createComponents(app)           % Register the app with App Designer           registerApp(app, app.UIFigure)           if nargout == 0               clear app           end       end       % Code that executes before app deletion       function delete(app)           % Delete UIFigure when app is deleted           delete(app.UIFigure)       end   endend

  • 发表于:
  • 原文链接https://page.om.qq.com/page/Oj74tex8RCbGnyi1X5Ya1s8Q0
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。
领券