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)
댓글 수: 0
채택된 답변
Walter Roberson
2022년 2월 15일
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
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.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!