Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #14049 > unrolled thread
| Started by | "Kevin J. McCann" <kjm@KevinMcCann.com> |
|---|---|
| First post | 2013-04-04 17:10 +0000 |
| Last post | 2013-04-05 02:29 +0000 |
| Articles | 3 — 2 participants |
Back to article view | Back to comp.soft-sys.math.mathematica
Problem with function definition inside DynamicModule "Kevin J. McCann" <kjm@KevinMcCann.com> - 2013-04-04 17:10 +0000
Re: Problem with function definition inside DynamicModule debguy <johnandsara2@cox.net> - 2013-04-05 02:29 +0000
Re: Problem with function definition inside DynamicModule debguy <johnandsara2@cox.net> - 2013-04-05 02:29 +0000
| From | "Kevin J. McCann" <kjm@KevinMcCann.com> |
|---|---|
| Date | 2013-04-04 17:10 +0000 |
| Subject | Problem with function definition inside DynamicModule |
| Message-ID | <kjkc64$7n6$1@smc.vnet.net> |
I am trying to write a dynamic module that does a polynomial fit with
different orders of the polynomial. I define f[x], the fit within the
module, but it does not update. If I try a delayed equals in the
definition, I get lots of unpleasant error messages. Any help would be
appreciated.
Thanks,
Kevin
data = Table[{x, x^2 + RandomReal[{-1, 1}]}, {x, 0, 5}];
datafit[dat_?ListQ] :=
DynamicModule[{m, d, f, x, y, k, n = 1, p1, powers},
p1 = ListPlot[dat, Joined -> False,
PlotStyle -> {Red, AbsolutePointSize[7]}, PlotRange -> All];
(* Here is where I think the problem is *)
f[x_] = Fit[dat, Table[x^k, {k, 0, n}], x];
Panel[
Column[
{
Row[{"n", Spacer[5], SetterBar[Dynamic[n], Range[5]]}],
Dynamic@
Plot[f[x], {x, 0, 5}, Epilog -> p1[[1]],
PlotLabel -> "n = " <> ToString[n],
PlotRange -> {0, 30}]
}
](*Column*)
](*Panel*)
]
datafit[data]
[toc] | [next] | [standalone]
| From | debguy <johnandsara2@cox.net> |
|---|---|
| Date | 2013-04-05 02:29 +0000 |
| Message-ID | <kjlcup$b5u$1@smc.vnet.net> |
| In reply to | #14049 |
I have don't have Dynamic to test with in my version, but this might
help.
I think
Dynamic@Plot[f[x], {x, 0, 5}, Epilog -> p1[[1]]
should update if p1 changes but not x. "f" is a question. I think you
said it does but f[x] does not. Try this:
f[x_, y_] = Fit[y, Table[x^k, {k, 0, n}], x];
This definition doesn't bury dat inside f. This may insure Dynamic
chooses the new f.
Also I'm thinking to update plot automatically when dat changes you
want to define datafit to use Dynamic[dat] rather than using dat as a
parameter. That might be a simpler approach.
Having used Dynamic[dat] in datafit you can then have your update by
parameter with simply:
datafitupdate[x_]:=dat=x;
[toc] | [prev] | [next] | [standalone]
| From | debguy <johnandsara2@cox.net> |
|---|---|
| Date | 2013-04-05 02:29 +0000 |
| Message-ID | <kjlcuu$b60$1@smc.vnet.net> |
| In reply to | #14049 |
datafit[]:= DynamicModule[{n},
Panel[
Column[
{
Row[{"n", Spacer[5], SetterBar[Dynamic[n], Range[5]]}],
Dynamic@
Plot[ Fit[dat, Table[x^k, {k, 0, n}], x], {x, 0, 5},
Epilog -> ListPlot[dat, Joined -> False,
PlotStyle -> {Red, AbsolutePointSize[7]}, PlotRange -> All]
[[1]],
PlotLabel -> "n = " <> ToString[n],
PlotRange -> {0, 30}]
}
](*Column*)
](*Panel*)
];(*DynamicModule*)
datafit[];
dataupdate[x_]:=dat=x;
dataupdate[somedat];
I think the above will update if ever and when:
Global`dat changes or Slider's n changes
And I cannot test it though.
But notice I didn't make dat internal to DynamicModule because there
is no intended Slider inside datafit that would create itself within
the scope of DynamicModule to change dat inside the DynamicModule (the
right dat). I didn't use datafit[ dat_] since (a protected symbol dat
local to a function / arg) maybe Dynamic cannot track assignments to
(or from) or will track strangely.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.soft-sys.math.mathematica
csiph-web