Filter löschen
Filter löschen

Iterating to find value S

2 Ansichten (letzte 30 Tage)
Jason
Jason am 23 Feb. 2011
  • 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);

Antworten (1)

Andrew Newell
Andrew Newell am 23 Feb. 2011
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.
  1 Kommentar
Jason
Jason am 25 Feb. 2011
I'm very greatful for the response but I'm still very confused.
I didn't really explain what I'm trying to do very well. Hopefully this will be clearer:
1. Assume a value for D.
2. Work out V from V=Q/(W*D) (values Q,W are inputted)
3. Solve S=(w*Cts*10^(I/J))/(V-Vcr)
Where:
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)
Us = (9.81*R*S)^0.5;
if Us*d/v is between 1.2 and 70
Vcr=w*((2.5/(log10(Us*d/v)-0.06))+0.66)
Otherwise
Vcr=w*2.05
So Us and as a result I and J depend on S
4. Select another D and repeat the steps
5. Compare computed V*S values and find a minimum value
I tried the following code:
y = MyReverseEquation3(Cts,R,d,v,w,Q,W,V,S0,D)
V=Q/(W*D);
Us = (9.81*R*S)^0.5;
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);
y=(w.*Cts.*10.^(I/J))/(V-Vcr)-S;
g = @(S) f(Cts,R,d,v,w,Q,W,V,S);
S = fzero(g,S0);
end
I get the error code: ‘Undefined function or variable "S".’

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by