Hello. Anyone knows why the fminsearch does not work. Is it the way the code is structured ?
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Rachel Ong
am 15 Feb. 2022
Kommentiert: Rachel Ong
am 18 Feb. 2022
clear all
clc
S = 17.1
W = 1248.5*9.81
rho = 1.225
speedsound = 340.26
h = 0
% u = x(1)
% CL = x(2)
% x0 = [20,0]
qbar = 0.5*rho*x(1)^2*S
T = (3*( (7+ x(1)/speedsound)*200/3 + h/1000*(2*x(1)/speedsound - 11) ) )
CD = 0.03 + 0.05*x(2)^2
D = qbar*CD
ROC = (T-D)*x(1)/W
fun = @(x) -1* (T-D) * x(1) / W
x0 = [20,0]
x = fminsearch(fun,x0)
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 15 Feb. 2022
S = 17.1
W = 1248.5*9.81
rho = 1.225
speedsound = 340.26
h = 0
% u = x(1)
% CL = x(2)
% x0 = [20,0]
qbar = @(x) 0.5*rho*x(1)^2*S
T = @(x)(3*( (7+ x(1)/speedsound)*200/3 + h/1000*(2*x(1)/speedsound - 11) ) )
CD = @(x) 0.03 + 0.05*x(2)^2
D = @(x) qbar(x).*CD(x)
ROC = @(x) (T(x)-D(x)) .* (x(1) ./ W)
fun = @(x) -ROC(x)
x0 = [20,0]
x = fminsearch(fun,x0)
3 Kommentare
Walter Roberson
am 17 Feb. 2022
My recommendation is to always use .* and ./ instead of * and / except
- when you deliberately want to do algebraic matrix multiplication (inner product) in which case use *
- when you want to use matrix right divide A/B intended to mean approximately A * pinv(B) . The / operator is a least-squared fitting operator, not what you would tend to think of as a "division" operator.
- for simple multiplication or division by constants, it becomes more a matter of style. Personally I tend to use (for example) x/2 instead of x./2 or use 3/5 instead of 3./5, and when a constant multiplier is the first thing in an I might use * instead of .* such as 2*x instead of 2.*x . But the longer the expression is, the more likely I am to consistently use .* in all places. For example in the middle of x^2.*y.*sin(z).*2.*pi.*f I am not likely to switch to x^2.*y.*sin(z)*2.*pi.*f even though it would give the same result -- because I don't want the user to be stopping to think "Why did they suddenly switch to * here, does that mean something??" . When I am writing example code for newer uses, I tend to use .* all the time, to get them accustomed to the idea that most of the time they need .* . If I know that in context y is scalar, then I am still going to write x^2.*y.*sin(z).*2.*pi.*f instead of x^2*y.*sin(z).*2.*pi.*f because chances are too high that at some point later the user is going to substitute a non-scalar y into the expression and then suddenly have problems they can't explain.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!