フィルターのクリア

I have an error I got from using fsolve but I do not understand why.

1 回表示 (過去 30 日間)
Rachel Ong
Rachel Ong 2021 年 12 月 6 日
コメント済み: Rik 2021 年 12 月 6 日
clear all;
clc;
W = 1248.5*9.81;
S = 17.1;
K = 0.05;
CDe = 0.03;
h = 0;
[T, p, rho, speedsound] = atmos(h);
V = 100 % random speed for test
Tava = (4 * ( (7 + V/speedsound )*200/3 + h*(2*V/speedsound) - 11) )
% x(1) = CL
func_1 = @(x) ((x(1))^2)/K + Tava/W*x(1) -3*CDe ; % Condition for max RC
FUNC = @(x) [func_1(x)];
CL = fsolve(@(x) FUNC(x), [0])
CD = 0.03 + 0.05*CL^2
% x(2) = alpha
% x(3) = eledefl
func_2 = @(x) 6.44*x(2) + 0.355*x(3) - CL ; % CL
func_3 = @(x) 0.03 + 0.05*(6.44*x(2) + 0.355*x(3))^2 - CD ; % CD
FUNC = @(x) [func_2(x); func_3(x)];
X = fsolve(@(x) FUNC(x), [0 0])
%
% aoa = X(1)
% eledefl = X(2)
I can solve for x(1) but not x(2) and x(3).
I got this error "Index exceeds the number of array elements (2).", however I dont understand what it means.
How can I solve this?

採用された回答

Rik
Rik 2021 年 12 月 6 日
編集済み: Rik 2021 年 12 月 6 日
You have this in func_2: x(3). That means the x you're passing should be at least 3 elements long.
X = fsolve(@(x) FUNC(x), [0 0 0])
You should also replace clear all by clear or by clearvars.
  2 件のコメント
Rachel Ong
Rachel Ong 2021 年 12 月 6 日
I see! Thank you so much! I am confused as I thought if I used x(2) and x(3) it will be 2 variables.
Rik
Rik 2021 年 12 月 6 日
You are indexing a vector. The fact that you only use 2 of them later on is not relevant, as fsolve is not aware of this.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeNonlinear Optimization についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by