I have this code in MATLAB, it doesnt run,what is the wrong?
g=9.81;zo=50;vo=15;m=50;c=13;
z=@(t) -(zo+m/c*(vo+m*g/c)*(1-exp(-c/m*t))-m*g/c*t);
[xmin fval]=fminbnd(z,0,8)

답변 (2개)

Star Strider
Star Strider 2023년 6월 18일

0 개 추천

It runs here —
g=9.81;zo=50;vo=15;m=50;c=13;
z=@(t) -(zo+m/c*(vo+m*g/c)*(1-exp(-c/m*t))-m*g/c*t);
[xmin fval]=fminbnd(z,0,8)
xmin = 1.2874
fval = -59.1178
Does it throw any errors?
The fminbnd functon is part of core MATLAB and was introduced before R2006a, so you should have it.
.
John D'Errico
John D'Errico 2023년 6월 18일
편집: John D'Errico 2023년 6월 18일

0 개 추천

Never just say something does not run, and nothing more. Would you tell your doctor that something seems wrong, and hope they can diagnose your problem over the phone without saying anything more? Surely not.
If you got an error, then report the entire error, so EVERYTHING in red.
Does your code run? We can test it in Answers itself.
g=9.81;zo=50;vo=15;m=50;c=13;
z=@(t) -(zo+m/c*(vo+m*g/c)*(1-exp(-c/m*t))-m*g/c*t);
format long g
[xmin fval]=fminbnd(z,0,8)
xmin =
1.28739311227159
fval =
-59.1178285772914
I'll add a few more lines to see what it found.
fplot(z,[0,8],'b')
hold on
plot(xmin,fval,'ro')
So indeed, it does run, and appears to have found the minimum of that function. How well did it do?
syms T
Z = z(T)
Z = 
If it has a (local) minimum, then you can differentiate it, and solve for a solution.
tmin = solve(diff(Z,T,1) == 0,'returnconditions',true)
tmin = struct with fields:
T: - (50*log(327/457))/13 + (pi*k*100i)/13 parameters: k conditions: in(k, 'integer')
So there are infinitely many solutions, but all of them are complex, except for the one where k==0.
subs(tmin.T,'k',0)
ans = 
tmin = double(ans)
tmin =
1.28739699998827
To within the tolerances provided to fminbnd, AND the capabilities of floating point arithmetic in double precision, fminbnd found a reasonable solution.

카테고리

도움말 센터File Exchange에서 Signal Processing Toolbox에 대해 자세히 알아보기

태그

질문:

2023년 6월 18일

편집:

2023년 6월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by