필터 지우기
필터 지우기

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 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