Fzero Initial value issues

조회 수: 10 (최근 30일)
Ethan Utter
Ethan Utter 2022년 12월 3일
댓글: John D'Errico 2022년 12월 4일
hey guys getting an error when i try to run this ik the value is around 247 but im getting an error of
"Error using fzero
Initial function value must be finite and real."
%% Part A section II
dhdt = @(t,h) (((-100*r.^2)*sqrt(2*g*h))./(2*h*R-h.^2));
[t2,h2] = ode45(dhdt,[0:600],6.5);
plot(t2,h2);
axis([0 600 0 7])
fun = @(x) polyval(polyfit(t2,h2,1),x)-.5
minT = fzero(fun,245)
  댓글 수: 1
John D'Errico
John D'Errico 2022년 12월 4일
Since you never tell us the value of r or g, we cannot even reproduce what you did. If you want help, it would be a good thing to make it easy to get help.

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

답변 (1개)

Walter Roberson
Walter Roberson 2022년 12월 3일
fun = @(x) polyval(polyfit(t2,h2,1),x)-.5
Why are you redoing the polyfit each time? You are not changing t2 or h2 or 1 inside fun so polyfit() is always going to return the same value, so you should pre-compute it.
You are fitting a straight line, getting out coefficients for something of the form a*x+b and you are wanting to find x such that a*x+b == 0.5 . But you can solve that directly without fzero, as x = (0.5-b)./a .
p = polyfit(t2, h2, 1);
x = (0.5 - p(2))./p(1);

카테고리

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

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by