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

 채택된 답변

0 개 추천

参考代码:
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!