matlab中,用循环结构求最小问题。

matlab中,求正整数对p,q,使得p+q<=2022且|p\q-0.915|最小,除了用整数非线性规划求解外,用循环结构的话应该怎么用matlab编程呢?

 采纳的回答

参考代码:
clear;clc
b = 1e9*ones(2021,2021);
for p = 1:2021
    for q = 1:2021
        if p+q<=2022
            b(p,q) = double(abs(p/q-0.915));
        else
            b(p,q) = 1e9;
        end
    end
end
[min_col,min_col_index] = min(b); %找到b数组中,每列的最小值,最小值在每列的位置
[min_b,min_b_index] = min(min_col); %找到每列最小值中的最小值,及其所在的位置(也即b数组中所有元素中最小值所在的列数)
b_index = min_col_index(min_b_index); %返回b数组中所有元素中最小值所在的行数
% 验证所求p,q是否满足条件
p = b_index;
q = min_b_index;
q+q
p/q-0.915

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 MATLAB 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!