Mathematica Looping Help: Find 'v' for 10 Different Values of 't

  • Context: Mathematica 
  • Thread starter Thread starter suraj goyal
  • Start date Start date
  • Tags Tags
    Mathematica
Click For Summary

Discussion Overview

The discussion revolves around a Mathematica coding problem where a user seeks assistance in creating a loop to calculate a variable 'v' for ten different values of 't'. The context involves defining variables that depend on 't', solving a quadratic equation, and plotting the relationship between 't' and 'v'.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Homework-related

Main Points Raised

  • The original poster (OP) describes a Mathematica code where 't' is fixed, and all other variables depend on it, leading to a desire to loop over multiple values of 't' to plot 'v' against 't'.
  • One participant suggests that 't' should not be set at the beginning and recommends expressing all variables as functions of 't'.
  • The OP shares an attempt using a For loop to iterate over 't', but notes that the output does not allow for proper plotting since the data is printed rather than stored.
  • Another participant advises against using explicit loops and suggests using Table to create a list of values for plotting instead.

Areas of Agreement / Disagreement

Participants generally agree that the approach to defining 't' and storing results needs adjustment, but there is no consensus on the best method to implement the solution.

Contextual Notes

The discussion highlights limitations in the OP's approach, particularly regarding the storage of computed values and the use of looping constructs in Mathematica. The need for clear function definitions and data handling is emphasized.

suraj goyal
Messages
2
Reaction score
0
pl help
Here I have written a mathematica code. It is a small example of my actual problem. In this code, 't' is an arbitrary constant which is being fixed at the top of the programme by assigning a value. All the other variables 'a','b','c' depend upon 't' . and 'h1','h2' are the roots of quadratic equation with coefficients a,b,c.
Further, 'f' is a function in an unknown variable 'p' and involves 'h1' and 'h2'.. We can determine 'p' by solving f(p)=0, giving two roots. Finally I calculated 'v' from the from assigned 't' and calculated second root of equation f(p)=0.
Thus 'v' depends upon 't' both explicitly and implicitly.. With change in 't', "v" will change. I want to draw a graph between 't' and 'v'.. For this I need looping or some other command to assign different values to 't' at the top of the programme.. Me unable to do that.. Pl if possible , help me in this regard
t := 3
a := t^2
b := (t + 1)*I
c := t^2 + 2*t*I + 1
h1 = (-b + Sqrt[b^2 - 4*a*c])/(2*a)
h2 = (-b - Sqrt[b^2 - 4*a*c])/(2*a)
a11 := Sqrt[p]*h1
a12 := h1*2
a21 := 3*h2^2
a22 := 4 + p
f[p_] = a11*a22 - a21*a12
h = sols = NSolve[f[p] == 0, p]
d = p /. sols[[2]]
v = t/Re[d]
Suppose I want to calculate my final answer for ten values of 't' (say 1,2,3...10) and finally to plot a graph between the final values of 'v' and the ten assigned values for 't'. I have tried a lot in this regard and have defined a function, used array and table commands too.. But unable to pick those ten values by my for loop command and to draw graph...
 
Physics news on Phys.org
You should not set t at the beginning. It seems that t is the argument to a function, so you should express everything in terms of functions of t:

a[t_] := t^2
b[t_] := (t + 1)*I
c[t_] := t^2 + 2*t*I + 1
h1[t_] := (-b[t] + Sqrt[b[t]^2 - 4*a[t]*c[t]])/(2*a[t])
...
 
Now I have tried the following

For[t = 1, t < 100, t = t + 10,
a = t^2;
b = (t + 1)*I;
c = t^2 + 2*t*I + 1;
h1 = (-b + Sqrt[b^2 - 4*a*c])/(2*a);
h2 = (-b - Sqrt[b^2 - 4*a*c])/(2*a);
a11 = Sqrt[p]*h1;
a12 = h1*2;
a21 = 3*h2^2;
a22 = 4 + p;
S = {{a11, a22}, {a21, a22}};
f[p_] = Det;
h = sols = NSolve[f[p] == 0, p];
d = p /. sols[[2]];
v = t/Re[d] ;
data = {{v, t}} // TableForm;
Print[data];
]
The output is
{{2.41743, 1}}
{{-2.75, 11}}
{{-5.25, 21}}
{{-7.75, 31}}
{{-10.25, 41}}
{{-12.75, 51}}
{{-15.25, 61}}
{{-17.75, 71}}
{{-20.25, 81}}
{{-22.75, 91}}
But problem is to plot.. In plotting command, it willl take the final value i.e. -22.75 and 91.. So still having the problem.. Want to plot a graph between 't' and 'v...'Pl help
 
Last edited:
Don't do it that way. Make everything into functions of t. Avoid explicit looping structures in interpreted languages whenever possible.

The reason you are unable to plot the data is because you never set anything equal to the data, you simply printed it. Printing it displays it but does not store it in a variable.

Instead of using For you should use Table to actually build up a table of values.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
Replies
1
Views
1K
  • · Replies 6 ·
Replies
6
Views
10K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 52 ·
2
Replies
52
Views
13K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 2 ·
Replies
2
Views
5K