Hello. Anyone knows why the fminsearch does not work. Is it the way the code is structured ?

2 Ansichten (letzte 30 Tage)
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)

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 15 Feb. 2022
S = 17.1
S = 17.1000
W = 1248.5*9.81
W = 1.2248e+04
rho = 1.225
rho = 1.2250
speedsound = 340.26
speedsound = 340.2600
h = 0
h = 0
% u = x(1)
% CL = x(2)
% x0 = [20,0]
qbar = @(x) 0.5*rho*x(1)^2*S
qbar = function_handle with value:
@(x)0.5*rho*x(1)^2*S
T = @(x)(3*( (7+ x(1)/speedsound)*200/3 + h/1000*(2*x(1)/speedsound - 11) ) )
T = function_handle with value:
@(x)(3*((7+x(1)/speedsound)*200/3+h/1000*(2*x(1)/speedsound-11)))
CD = @(x) 0.03 + 0.05*x(2)^2
CD = function_handle with value:
@(x)0.03+0.05*x(2)^2
D = @(x) qbar(x).*CD(x)
D = function_handle with value:
@(x)qbar(x).*CD(x)
ROC = @(x) (T(x)-D(x)) .* (x(1) ./ W)
ROC = function_handle with value:
@(x)(T(x)-D(x)).*(x(1)./W)
fun = @(x) -ROC(x)
fun = function_handle with value:
@(x)-ROC(x)
x0 = [20,0]
x0 = 1×2
20 0
x = fminsearch(fun,x0)
x = 1×2
39.1668 0.0000
  3 Kommentare
Walter Roberson
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.
Rachel Ong
Rachel Ong am 18 Feb. 2022
I see. Thank you for the very detailed explanation :) Very happy to be able to learn from you.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by