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

1 view (last 30 days)
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?

Accepted Answer

Rik
Rik on 6 Dec 2021
Edited: Rik on 6 Dec 2021
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 Comments
Rachel Ong
Rachel Ong on 6 Dec 2021
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 on 6 Dec 2021
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.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!