Re: Create and use a set of values for variables
- To: mathgroup at smc.vnet.net
- Subject: [mg107521] Re: [mg107519] Create and use a set of values for variables
- From: Bob Hanlon <hanlonr at cox.net>
- Date: Tue, 16 Feb 2010 03:50:28 -0500 (EST)
- Reply-to: hanlonr at cox.net
There are lots of different ways
y[x_] = a*x^2 + b*x + c;
sub[d_, e_, f_] = {b -> d + e, c -> 2 f, a -> d^2};
def = {{1, 4, 6}, {2, 3, 7}, {5, 5, 1}};
Method 1
y[x] /. sub[1, 4, 6]
x^2 + 5*x + 12
y[x] /. sub @@ # & /@ def
{x^2 + 5*x + 12, 4*x^2 + 5*x + 14,
25*x^2 + 10*x + 2}
Method 2
With[{d = 2, e = 3, f = 7}, y[x] /. sub[d, e, f]]
4*x^2 + 5*x + 14
With[{d = #[[1]], e = #[[2]], f = #[[3]]},
y[x] /. sub[d, e, f]] & /@ def
{x^2 + 5*x + 12, 4*x^2 + 5*x + 14,
25*x^2 + 10*x + 2}
Method 3
y2[d_, e_, f_][x_] = y[x] /. sub[d, e, f];
y2[5, 5, 1][x]
25*x^2 + 10*x + 2
(y2 @@ #)[x] & /@ def
{x^2 + 5*x + 12, 4*x^2 + 5*x + 14,
25*x^2 + 10*x + 2}
Bob Hanlon
---- Menl <szegersnl at gmail.com> wrote:
=============
Hi,
I try to find a method of doing the following in Mathematica.
I have some function y[x_]=a x^2 + b x + c
Now I want some list which hold values for a, b and c. that are dependend on some lower level values d, e and f.
vars1=[{d=1, e=4, f=6},{b=d+e, c=2f, a = d^2}]
vars2=[{d=2, e=3, f=7},{b=d+e, c=2f, a = d^2}]
vars3=[{d=5, e=5, f=1},{b=d+e, c=2f, a = d^2}]
and use these sets with the original function when I need it.
This line will not work as intended of course but I hope it makes it clear what I try to do. The key is I would like to not evaluate these values outside this vars, so the original function stays unevaluated.
Something like this I tried to do with either the functions Block or With, but both evaluate the values for a, b and c. I would like to make several such sets of variables without evaluating them so I can use them as desired, to plot the original function with several sets of vars.