Re: Simple question
- To: mathgroup at smc.vnet.net
- Subject: [mg28540] Re: Simple question
- From: "Allan Hayes" <hay at haystack.demon.co.uk>
- Date: Wed, 25 Apr 2001 19:21:48 -0400 (EDT)
- References: <9c5nhm$itb@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Urijah:
First, the use of rules
soln=Solve[{x^2+y^2\[Equal]4, x+y\[Equal]0},{x,y}]
{{x -> -Sqrt[2], y -> Sqrt[2]}, {x -> Sqrt[2], y -> -Sqrt[2]}}
This gives the two solutions for the pair x and y --- that's why we need a
list of lists.
The replacement rule form lets us put the solutions into expressions
x+y^2/.soln
{2 - Sqrt[2], 2 + Sqrt[2]}
The first number comes from the first solution, the second one from the
second solution.
If we want to use only the first solution we can get its list of rules out:
firstsoln = soln[[1]]
{x -> -Sqrt[2], y -> Sqrt[2]}
x+y^2/.firstsoln
2 - Sqrt[2]
Now for the interpolation functonsThe following example, taken from the
HelpBrowser entry for NDSolve, finds the functions x, and y that satisfy the
equations.
Let's take some points on the sine curve:
data = Table[{t, Sin[t]},{t,0.,6, .2}]
{{0., 0.}, {0.2, 0.198669}, {0.4, 0.389418}, {0.6, 0.564642}, {0.8,
0.717356},
{1., 0.841471}, {1.2, 0.932039}, {1.4, 0.98545}, {1.6, 0.999574},
{1.8, 0.973848}, {2., 0.909297}, {2.2, 0.808496}, {2.4, 0.675463},
{2.6, 0.515501}, {2.8, 0.334988}, {3., 0.14112}, {3.2, -0.0583741},
{3.4, -0.255541}, {3.6, -0.44252}, {3.8, -0.611858}, {4., -0.756802},
{4.2, -0.871576}, {4.4, -0.951602}, {4.6, -0.993691}, {4.8, -0.996165},
{5., -0.958924}, {5.2, -0.883455}, {5.4, -0.772764}, {5.6, -0.631267},
{5.8, -0.464602}, {6., -0.279415}}
Make an interpolation function going through these points
sn=Interpolation[data]
InterpolatingFunction[]
Check that it does interpolate
{sn[.2], sn[.4]}
{0.198669, 0.389418}
It is define for all numbers between 0 and 6, for example
sn[1.23]
0.942472
Compare with
Sin[1.23]
0.942489
Plot sn:
Plot[sn[t],{t,0,6}];
<picture deleted>
Differentiate sn
cs[t_]=D[sn[t],t]
InterpolatingFunction[][t]
Plot[cs[t],{t,0,6}]
<picture deleted>
Differentiate cs -- a different way --- we should get -sn
ssn = cs'
(InterpolatingFunction[])'[#1] &
Plot[ssn[t],{t,0,6}]
<picture deleted>
soln = NDSolve[{x'[t] == y[t], y'[t] == -0.01*y[t] - Sin[x[t]], x[0] == 0,
y[0] == 2.1}, {x, y}, {t, 0, 100}]
{{x -> InterpolatingFunction[], y -> InterpolatingFunction[]}}
solution gives the unique solution.
Take it out
firstsoln = soln[[1]]
{x -> InterpolatingFunction[], y -> InterpolatingFunction[]}
Now let's plot the solution.
xy[t_]={x[t],y[t]}/.soln
{{InterpolatingFunction[][t], InterpolatingFunction[][t]}}
This replaces x with the function that is on the right of
x\[Rule]InterpolatingFunction[{{0.,100.}},<>], and replaces y with the
function on the right of y\[Rule]InterpolatingFunction[{{0.,100.}},<>]
If I replace t with 1.2 then the functions will evaluate
xy[t]/.t\[Rule]1.2
{{2.0674, 1.17637}}
Similarly, if I define t = 1.2
t=1.2
1.2
Then this value is used
fs
{{2.0674, 1.17637}}
Now we can plot the solution (don't worry about the warning message that you
will get)
ParametricPlot[xy[t],{t,0,100}];
<picture deleted>
All that is going on here is that ParametricPlot is assigning numerical
values to t, so the it evaluates as we saw, and using these to draw the
graph.
The HelpBrowser does this more directly:
ParametricPlot[Evaluate[{x[t],y[t]}/.soln],{t,0,100}];
--
Allan
---------------------
Allan Hayes
Mathematica Training and Consulting
Leicester UK
www.haystack.demon.co.uk
hay at haystack.demon.co.uk
Voice: +44 (0)116 271 4198
Fax: +44 (0)870 164 0565
"Urijah Kaplan" <uak at sas.upenn.edu> wrote in message
news:9c5nhm$itb at smc.vnet.net...
> I'm new to Mathematica, and I'm using it to solve numerical differential
> equations. I get answers like{{x -> InterpolatingFunction[{{0., 50.}},
> "<>"], y -> InterpolatingFunction[{{0., 50.}}, "<>"]}} How do I use them?
> The help files say I can treat them as a regular function, but I'm not
sure
> how I can set it in a x[t] form, and then pick my values of t. Can anyone
> help me?
>
>
> --Urij
ah
> Kaplan
>
>
>