How do I solve for a value that depends on another variable....

5 Ansichten (letzte 30 Tage)
EE
EE am 28 Mär. 2016
Kommentiert: EE am 28 Mär. 2016
Hello,
I m trying to get Ilmax which makes xs zero.
To get xs I need to solve first for xint_cg
I tried several ways...different errors
With this routine
syms xint1;
syms xint2;
syms yc yg xc xg Sc Sg;
ycl_p=yc+sqrt(Sc^2-(xint2-xc)^2);
ygl_p=yg+sqrt(Sc^2-(xint2-xg)^2);
Sol_p=solve(ycl_p-ygl_p,xint2);
xint_cg=(Sol_p)
yc=27.95;
yg=34.13;
xc=9.15;
xg=4.73;
Sc=51.64;
Sg=29.87;
k=1000:1:50000;
for Il=1:numel(k)
Sc(Il)=10*(Il)^0.65;
Sg(Il)=(3.6+1.7*log(43-yc))*(Il)^0.65;
xint_g_p(Il)=xc+sqrt(Sc(Il)^2-(yc-Sg(Il)^2));
e1(Il)=yc+sqrt(Sc(Il)^2-(xint_cg(Il)-xc)^2);
e2(Il)=yg+sqrt(Sc(Il)^2-(xint_cg(Il)-xg)^2);
SOl(Il)=solve(e1(Il)-e2(Il),xint_cg(Il));
xs(Il)=xint_g_p(Il) - xint_cg_p(Il)
if xs(Il)==0
k=Ilmax
end
end
xint_cg
k

Antworten (1)

Walter Roberson
Walter Roberson am 28 Mär. 2016
Sol_p=solve(ycl_p-ygl_p,xint2); is going to have two results. You then assign it to xint_cg, so xint_cg is going to have length 2. But your "for Il" loop tries to index it up to numel(k) which is 49001. That would fail as soon as I1 became 3.
In the line
SOl(Il)=solve(e1(Il)-e2(Il),xint_cg(Il));
xint_cg(I1) is going to be an expression, and e1(Il)-e2(Il) is going to be an expression, so the solve() will be trying to solve two expressions simultaneously. Which two variables are you expecting to solve over? Even if the expressions do happen to have exactly two free variables there, the code would be so much more understandable if you named the specific variables there. Do not make life harder than it needs to be for people who are reading your code.
You are trying to find the Ilmax that makes xs 0, but nothing in your code sets I1max or uses it (other than that one line), so we do not know what you are looking for. Do you perhaps want to assign Il there? Are you looking for the Il that makes xs 0? Is there reason to expect that the solution will be an integer?
Why are you defining
k=1000:1:50000;
and iterating Il from 1 to numel(k), but your only other reference to k is to assign Ilmax to it? Is your code possibly needing to refer to k(Il) in those places it is referring directly to Il (other than as a subscript) ?
When you do assign the (presumably scalar) Ilmax to k, are you expecting that the "for Il=1:numel(k) will notice the change to the size of k and so exit the loop, numel(k) having become 1? Because that is not how "for" loops work: for loops record copies of the loop parameters and use those copies no matter how any variables might change in the loop.
Note: it is not possible to solve() with respect to an expresion, only with respect to a variable.
  1 Kommentar
EE
EE am 28 Mär. 2016
Hello Walter, Thank you for you time. I m trying to ultimately solve for the value of Il that makes xs zero. xint_cg will be a number, I get a number if I solve for it this way:
yc=27.95; yg=34.13; xc=9.15; xg=4.73; Sc=51.64; Sg=29.87; %%
syms xint_cg;
ycl_p=yc+sqrt(Sc^2-(xint_cg-xc)^2);
ygl_p=yg+sqrt(Sc^2-(xint_cg-xg)^2);
Sol_p=solve(ycl_p-ygl_p,xint_cg);
xint_cg=eval((Sol_p))
The result is:
xint_cg = 48.8290
Once I get xint_cg my goal is to find the value of Il that makes xs zero. But in order to do that different values of Sc and Sg and consequntly xint_cg needs to be found. So after I get xint_cg I check for xs, if is zero then that Il is Ilmax. If not I need to increase Il and find new Sc, Sg,xint_cg, recheck for xs.... The solution can be any positive number, not just integer. With this code, it runs, but I get no answer, xs in not zero. Il=1:60;
for k=1:length(Il)
Sc=10*Il(k)^0.65;
Sg=(3.6+1.7*log(43-yc))*Il(k)^0.65;
xint_g_p=xc+sqrt(Sc^2-(yc-Sg^2));
e1=yc+sqrt(Sc^2-(xint_cg-xc)^2);
e2=yg+sqrt(Sc^2-(xint_cg-xg)^2);
sol=(e1-e2);
if sol~=0
end
xs=xint_g_p - xint_cg;
if xs~=0
end
end
k
Thank you
Enrico

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Programming 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