How can I fix this error? 'Error: File: RSM_yeni.m Line: 84 Column: 19 Invalid array indexing.'

3 ビュー (過去 30 日間)
Seyda Ozturk
Seyda Ozturk 2024 年 8 月 29 日
編集済み: Steven Lord 2024 年 8 月 29 日
% Giriş (input) ve çıkış (output) verileri
input_data = [
% LengthofBody Diameter ConicLength Mach
1.25, 0.25, 0.5, 0.85;
2.75, 0.25, 0.5, 0.85;
1.25, 0.45, 0.5, 0.85;
2.75, 0.45, 0.5, 0.85;
2.0, 0.35, 0.35, 0.70;
2.0, 0.35, 0.65, 0.70;
2.0, 0.35, 0.35, 1.0;
2.0, 0.35, 0.65, 1.0;
2.0, 0.35, 0.50, 0.85;
1.25, 0.35, 0.5, 0.7;
2.75, 0.35, 0.5, 0.7;
1.25, 0.35, 0.5, 1.0;
2.75, 0.35, 0.5, 1.0;
2.0, 0.25, 0.35, 0.85;
2.0, 0.45, 0.35, 0.85;
2.0, 0.25, 0.65, 0.85;
2.0, 0.45, 0.65, 0.85;
1.25, 0.35, 0.35, 0.85;
2.0, 0.35, 0.35, 0.85;
1.25, 0.35, 0.65, 0.85;
2.75, 0.35, 0.65, 0.85;
2.0, 0.25, 0.50, 0.70;
2.0, 0.45, 0.5, 0.70;
2.0, 0.25, 0.5, 1.0;
2.0, 0.45, 0.5, 1.0;
];
output_data = [
% C_M (Constraint) C_D (Min)
-0.005, 0.053;
-0.005, 0.025;
0.003, 0.125;
0.006, 0.053;
-0.012, 0.051;
-0.007, 0.039;
-0.001, 0.116;
0.002, 0.081;
-0.008, 0.050;
0.007, 0.039;
0.010, 0.029;
0.001, 0.084;
0.001, 0.068;
-0.004, 0.030;
-0.002, 0.091;
0.001, 0.030;
-0.001, 0.071;
0.001, 0.010;
-0.001, 0.067;
-0.006, 0.082;
-0.007, 0.035;
-0.002, 0.028;
0.009, 0.055;
-0.001, 0.059;
0.001, 0.118;
];
% Quadratic Response Surface Modeli oluşturma
mdl1 = fitlm(input_data, output_data(:,1), 'quadratic'); % C_M için model
mdl2 = fitlm(input_data, output_data(:,2), 'quadratic'); % C_D için model
% Modelin değerlendirilmesi
disp('C_M için Model Özeti:');
disp(mdl1);
disp('C_D için Model Özeti:');
disp(mdl2);
% Tahmin fonksiyonları
fun = @(x) [predict(mdl1, x); predict(mdl2, x)]; % C_M ve C_D için tahmin fonksiyonları
% Başlangıç noktası (Input değişkenleri için başlangıç tahmini)
x0 = [2.0, 0.35, 0.5, 0.85]; % Varsayılan başlangıç noktası
% Optimizasyon seçenekleri
options = optimset('Display', 'iter');
% Amaç fonksiyonu: C_D'yi minimize ediyoruz (Output2)
objective = @(x) fun(x); % Tüm çıktıları almak için
% Kısıt fonksiyonu: C_M için bir kısıtlama belirliyoruz
% Örneğin C_M <= -0.005
constraint = @(x) fun(x)(1) - (-0.005); % C_M - (-0.005) <= 0 olmalı
% Kısıtlı optimizasyon: fmincon kullanılıyor
[x_opt, fval] = fmincon(@(x) fun(x)(2), x0, [], [], [], [], [], [], constraint, options);
% Sonuçları görüntüleme
disp('Optimum Input Değerleri:');
disp(x_opt);
disp('Optimum Output Değerleri:');
disp(['C_D (Minimize Edilen): ', num2str(fval)]);
disp(['C_M (Kısıt): ', num2str(fun(x_opt)(1))]);

回答 (1 件)

Steven Lord
Steven Lord 2024 年 8 月 29 日
Looking at this line in your code (commenting it out so I can run code later in this answer)
%{
constraint = @(x) fun(x)(1) - (-0.005); % C_M - (-0.005) <= 0 olmalı
%}
You can't chain indexing with parentheses like fun(x)(1) in MATLAB. What you could do is create an auxiliary function:
elementN = @(array, n) array(n);
then call it:
fun = @sin; % dummy objective function
constraint = @(x) elementN(fun(x), 1);
To check that evaluating constraint gives the first element of the output of fun:
y1 = fun(1:10)
y1 = 1x10
0.8415 0.9093 0.1411 -0.7568 -0.9589 -0.2794 0.6570 0.9894 0.4121 -0.5440
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
y2 = constraint(1:10)
y2 = 0.8415
y1(1) == y2 % true
ans = logical
1
To prove that you can't chain indexing:
thisWillThrowAnError = fun(1:10)(1)
Invalid array indexing.
Also FYI, the next time you post an error message please include it in message itself instead of or in addition to including it in the title of your question, and if it refers to a specific line of your code please indicate somehow (with a comment or in text after the code) which line that is. It makes it easier to locate the source of the error.
  2 件のコメント
Seyda Ozturk
Seyda Ozturk 2024 年 8 月 29 日
Thank you for your feedback, but I couldn't make the corrections you said. Because I'm very new to Matlab. Could you help me?
Walter Roberson
Walter Roberson 2024 年 8 月 29 日
編集済み: Steven Lord 2024 年 8 月 29 日
Change
constraint = @(x) fun(x)(1) - (-0.005); % C_M - (-0.005) <= 0 olmalı
to
elementN = @(array, n) array(n);
constraint = @(x) elementN(fun(x), 1) - (-0.005);
[SL: added the second term in the second constraint function.]

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLeast Squares についてさらに検索

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by