Reusing a calculated variable twice?

1 次查看(过去 30 天)
Joshua
Joshua 2014-3-12
Hi all, I'm trying to run a series of calculations with multiple variables used multiple times to collect different values. So far I have this:
clear
g=9.81;
h=10;
u=1.5;
syms a b c d
a=u./sqrt(g.*h)
h=10;
d=0.5;
c=0.5;
b=solve(((a^(2).*b^4)/2)+2.*c.*a^(2).*b^(3)-(2-2.*d+a^(2)).*b^(2)-(4.*c+2.*c.*a^(2)-4).*b+((a^(2)/2)+4.*c-2.*c^(2).*d-2),b,'MaxDegree',3);
x=b(find(b>1))
e1=((2.*(x(1)+c))-((x(1)-1)^3)/(d.*x(1).*(x(1)-c)))/(4+((((x(1))^(2))-1)/(c.*x(1))))
e2=((2.*(x(2)+c))-((x(2)-1)^3)/(d.*x(2).*(x(2)-c)))/(4+((((x(2))^(2))-1)/(c.*x(2))))
if (e1>c)
if (1>e1)
f1=(x(1))^2-e1^2
end
end
if (e2>c)
if(1>e2)
f2=(x(2))^2-e2^2
end
end
g=f2/(e2)^2
syms h1 a d f2
h1=solve(((h1^3)./2)-((3/2).*h1^2)+(1-a^2+(f2.*d.*(a^2))./2).*h1-((f2.*d.*(a^2))/2),h1,'MaxDegree',3)
The code works as expected except the last line in which it gives me 3 answers in terms of a rather than a numerical answer. How do I make it reuse the value calculated for a like it did so in the initial solve line? Many thanks

回答(1 个)

Marta Salas
Marta Salas 2014-3-12
编辑:Marta Salas 2014-3-12
when you use "syms" is to create symbolic variables.
clear
g=9.81;
h=10;
u=1.5;
syms a b c d
a=u./sqrt(g.*h)
h=10;
d=0.5;
c=0.5;
you are not using these symbolic variables (a b c d) because you assigned values to them. So, remove the "syms" lines, you will get a numeric solution.
NOTE: to get a numeric solution you will need to assign first the value of h1, which in you previous code is not defined.

产品

Community Treasure Hunt

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

Start Hunting!

Translated by