Iterating to find value S
1 次查看(过去 30 天)
显示 更早的评论
- Hello I'm fairly new to Matlab and I'm having trouble iterating. I'm trying to find the value S however the equation needed to evaluate it depends on S (second line). What is the best way to find S? Any help would be greatly appreciated :)My code:
V=Q/(W*R); Us = (9.81*R*S)^0.5; if ((Us*d/v)>1.2) && ((Us*d/v)<70); Vcr=w*((2.5/(log10(Us*d/v)-0.06))+0.66); else Vcr=w*2.05; end
I = 5.435-0.286*log10(w*d/v)-0.457*log10(Us/w); J = 1.799-0.409*log10(w*d/v)-0.314*log10(Us/w); S=(w*Cts*10^(I/J))/(V-Vcr);
0 个评论
回答(1 个)
Andrew Newell
2011-2-23
Jason, here is a sketch of what you need to do. First, you need to create a function
y = f(S,Q,W,etc.)
in a separate file will all your independent variables as arguments (there seem to be quite a few). This function will need to accept a vector for S, which means a lot of your code will need to be rewritten. In particular, the if/then statement could be put in side a loop or replaced by logical indexing (see Matrix indexing in MATLAB):
Vcr = w*2.05;
ix = ((Us*d/v)>1.2) && ((Us*d/v)<70);
Vcr(ix) = w*((2.5/(log10(Us(ix)*d/v)-0.06))+0.66); %EDITED
and you need to replace * by .* and ^ by .^ everywhere (assuming that all the other variables are scalars). The last line of the function would be
y=(w*Cts*10^(I/J))/(V-Vcr)-S;
The solution of the equation y=0 is the S you are after.
Then define an inline function
g = @(S) f(S,Q,W, ...)
(see Anonymous functions). You need to provide values for all the arguments except S before defining this function. Finally, solve using fzero:
S = fzero(g,S0)
where S0 is your guess for the value of S that solves the equation. To help you with finding a good guess, you could plot g(S) against S first.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!