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

1 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Nonlinear Optimization 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by