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

조회 수: 2 (최근 30일)
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)

채택된 답변

Walter Roberson
Walter Roberson 2022년 2월 15일
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
Walter Roberson
Walter Roberson 2022년 2월 17일
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 2022년 2월 18일
I see. Thank you for the very detailed explanation :) Very happy to be able to learn from you.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by