Message Boards Message Boards

0
|
4649 Views
|
4 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Dynamic [x] with units not solving to a number ? Help ;-)

Posted 9 years ago

Folks,

Can't see to get ff= to show just a single value ?

I would like to input with a slider a vlaue (with units) and let it dynamiclly update in other equations (MAthCAD Like ?)

Please take a look at my code--any help would be greatly appreciated.

ClearAll["Global`*"]


II = t h^3/12;
Kbeam = 3 Esteel II/(l^3);
keff = 2*Kbeam (* two uprights *);
Print ["\!\(\*SubscriptBox[\(I\), \(beam\)]\)=", 
 TraditionalForm[II], ", \!\(\*SubscriptBox[\(K\), \(beam\)]\)=", 
 TraditionalForm[
  Kbeam], ", \!\(\*SubscriptBox[\(K\), \(eff\)]\)=", keff]
m = Quantity[10., "Pounds"];
r = Quantity[5., "Feet"]
g = Quantity[32., "Feet"/"Seconds"^2];
t = Quantity[8., "Inches"];
l = Quantity[9., "Feet"]*Quantity[12., "Inches"/"Feet"];
h = Quantity[16., "Inches"];


Esteel = Quantity[30. 10^6, "PoundsForce"/"Inches"^2];
Print["g=", g, ", t=", t, ", l=", l, ", h=", h, ", \
\!\(\*SubscriptBox[\(E\), \(steel\)]\)=", Esteel]
Print["\!\(\*SubscriptBox[\(I\), \(beam\)]\)=", II, ", keff=", 
 ScientificForm[keff, 3]]
Print["keff=", UnitConvert[Quantity[keff], "Newtons"/"Meters"]]
Slider[Dynamic[v0]]
Print ["v=", v = Dynamic[v0]*Quantity[1., "Feet"/"Seconds"]]
ff = m*(v^2)/r;
Print ["ff= ", ff]
Print["Why above is not solving ? for actual number ? The quantities \
are correct"]

Regards,

John

POSTED BY: john massucci
4 Replies
Posted 9 years ago

Again, thank you for your help -John

POSTED BY: john massucci
Posted 9 years ago

Ok, thanks for the tip (I'll be clearer )--Attached is the code See two questions below

Attachments:
POSTED BY: john massucci

Here is the simplified version of your problem:

{Slider[Dynamic[x], {1, 10}], Dynamic[x]}
Print [2*Dynamic[x]]

The problem you see is that the number "2" and the value of x are not multiplied before being printed. Why does this happen? After all Print[2*3] produces 6. But you also see that Print[2*Dynamic[3]] produces "2 3".

Dynamic is somewhat tricky. You should use Manipulate whenever possible. After that, you should use DynamicModule as much as possible. What you want here is:

 Print [Dynamic[2*x]]]

Do not put Dynamic around all instances of x. Only put dynamic around what is going to be output that needs to change. Additionally, you will need to use SetDelayed to allow some values to be recomputed. Your code would look like:

{Slider[Dynamic[x], {1, 10}], Dynamic[x]}
v := x*Quantity[1, "Feet"/"Seconds"];
m = Quantity[5.0, "Pounds"];
ff := m*(v^2);
Print ["ff= ", Dynamic[ff]]

This is somewhat complicated to understand. But it can be made easier if you follow the golden rule.

If some value changes because of another value, you should have a function.

Newer programmers over-use Print. You almost always want to return a value, not print it.

Here is how I would probably write your code:

{Slider[Dynamic[x], {1, 10}], Dynamic[x]}
v[x_] := Quantity[x, "Feet"/"Seconds"];
m = Quantity[5.0, "Pounds"];
ff[m_, v_] := m*(v^2);
Dynamic[ff[m, v[x]]]

The real lesson is that you should use Manipulate. It makes all of this much cleaner

POSTED BY: Sean Clarke

I am not sure I understand what you are asking. Can you try removing the extraneous parts of your code. Just include enough code to show the problem you are seeing. Try to make it as simple as possible. Make a simple example with just a couple of lines instead of this example which has many variables.

You asked:

Can't see to get ff= to show just a single value ?

Can you explain futher what you mean? What kind of behavior do you see? What kind of behavior do you want?

In the code you asked:

Why above is not solving ? for actual number ? The quantities are correct"

I'm not sure what this is asking. Can you be more precise about what you mean?

POSTED BY: Sean Clarke
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract