Mathematica -> making multiple variables in one go?

Click For Summary

Discussion Overview

The discussion revolves around the creation of multiple variables in Mathematica dynamically, particularly in the context of needing to generate a large number of variables based on a formula or data-driven parameters. Participants explore various methods for automating this process without manually declaring each variable.

Discussion Character

  • Technical explanation
  • Exploratory
  • Homework-related

Main Points Raised

  • One participant inquires about the possibility of dynamically creating multiple variables (e.g., X1, X2, ..., X200) based on a formula without manually declaring each one.
  • Another participant suggests using indexed variables such as x[1], x[2], etc., as an alternative approach.
  • A participant reports successfully using a For loop to assign values to variables like X[i].
  • Another participant proposes a method to programmatically construct variable symbols using the Symbol function and string manipulation.
  • A different participant raises a related issue about generating parameters and object labels for a nonlinear fit based on a variable number of objects, seeking advice on including these dynamically generated labels in a fitting function.
  • One participant provides a solution involving the use of Table and ToExpression to create parameter and variable lists for use in a NonlinearFit function, emphasizing the need to avoid previously assigned values to those variables.
  • A final participant expresses gratitude for the provided solution, indicating it met their needs.

Areas of Agreement / Disagreement

Participants generally agree on the feasibility of dynamically creating variables and parameters in Mathematica, with multiple approaches discussed. However, there is no consensus on a single best method, as various techniques are proposed and explored.

Contextual Notes

Some participants note the importance of ensuring that variables have not been previously assigned values, which could affect the dynamic creation process. There is also an implicit assumption that participants are familiar with Mathematica's syntax and functions.

Mugged
Messages
103
Reaction score
0
[ANSWERED] Mathematica -> making multiple variables in one go?

Ok so let's say i know how many variables i want:

numberofvariables = 5;

and then i want to create 5 different variables, say X1, X2, X3, X4, X5 based on some formula, say:

Xi = 5+i^2

but i want this creation of variables to be done automatically, without me having to explicity type out and declare X1, X2, X3,...separately...

is this possible?

I really need to know, because say i want numberofvariables = 200, it would be a pain to have to separately declare X1 through X200...

thank you so much.

(Note: this is a radically simplified version of what i am doing...there is not need to substitute a different method to solve the above problem, i just need dynamic variable declaration?)
 
Last edited:
Physics news on Phys.org
Are you really, really, really sure that you can't use variables such as x[1], x[2], x[3], ...?
 
Oh i was able to create a For loop and assign X variables like that...works

thank you very much Hurkyl
 
You can also construct the Xi symbols programmatically:

Do[Evaluate[Symbol["x" <> ToString]] = 5 + i^2, {i, 5}]
 
I sort of have a similar issue.

So if I was to do some nonlinear fit of N objects (x1 through xN) with parameters A for each, A1, A2, A3 ... AN, but the number of objects is dependent on what data I'm looking at. Is there some way to generate those parameters/object labels depending on the number of objects I have?

So if I write:
NonlinearFit[data, formula, {A1, A2, ... AN}, {x1, x2... xN}]

I can create the formula by joining strings together and then converting to an expression, the problem I have is somehow including the A1...AN parameters and x1... xN variables given some N.

Thanks for any advice, this has been bugging me the past year.
 
Create the parameters and variables almost the same way you create the formula.

In[1]:=n=12;
params=Table[ToExpression["A"<>ToString],{i,n}];
vars=Table[ToExpression["x"<>ToString],{i,n}];
(* now look at the result *)
Print[params,vars]

From In[1]:={A1,A2,A3,A4,A5,A6,A7,A8,A9,A10,A11,A12}{x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12}

Then you can write

NonlinearFit[data, formula, params, vars]

Just make certain you have not previously assigned values to any Ai or xi.
If you have assigned values to any of those you may want to look at Clear[].
 
Thanks! That's exactly what I needed.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 26 ·
Replies
26
Views
3K
  • · Replies 8 ·
Replies
8
Views
5K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K