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

  • Mathematica
  • Thread starter suraj goyal
  • Start date
  • Tags
    Mathematica
In summary: Next, you can use the Solve command to calculate the values of f(p) for various values of t. You can then plot these values on a graph using the Plot command.
  • #1
suraj goyal
2
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
  • #2
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])
...
 
  • #3
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:
  • #4
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.
 
  • #5


Thank you for sharing your Mathematica code and explaining your problem. It seems like you are trying to plot a graph of 'v' against different values of 't'. To do this, you can use a for loop to iterate through the ten different values of 't' and calculate the corresponding 'v' for each value. Here is an example code:

tValues = Range[1, 10]; (*assigning ten values to t*)
vValues = {}; (*creating an empty list to store v values*)

For[t = 1, t <= 10, t++, (*for loop to iterate through t values*)
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];
AppendTo[vValues, v]; (*adding calculated v value to the list*)
]

ListPlot[Transpose[{tValues, vValues}], PlotStyle -> PointSize[Large],
AxesLabel -> {"t", "v"}] (*plotting the values as a scatter plot*)

I hope this helps you in plotting your desired graph. If you face any further difficulties, please do not hesitate to ask for help. Good luck with your research!
 

1. What is a loop in Mathematica?

A loop in Mathematica is a programming construct that allows for repetitive execution of a block of code. It can be used to repeat a certain operation or calculation for a specified number of times or until a certain condition is met.

2. How do I create a loop in Mathematica?

To create a loop in Mathematica, you can use the "Do" or "For" functions. The "Do" function is used to repeat a certain block of code a specified number of times, while the "For" function is used to iterate through a range of values. Both functions require a starting value, an ending value, and an increment or decrement value.

3. How can I use a loop to find 'v' for 10 different values of 't'?

To find 'v' for 10 different values of 't' using a loop in Mathematica, you can first define a list of 10 values for 't' using the "Table" function. Then, you can use a "Do" or "For" loop to iterate through the list of 't' values and calculate 'v' for each value.

4. Can I use a loop to find 'v' for an infinite number of values of 't'?

Yes, you can use a loop in Mathematica to find 'v' for an infinite number of values of 't'. To do this, you can use a "While" or "DoWhile" loop, which will continue to execute a block of code until a certain condition is met. In this case, the loop would continue to calculate 'v' until a specific value of 't' is reached.

5. Are there any alternative methods to looping in Mathematica?

Yes, there are alternative methods to looping in Mathematica, such as using the "Map" or "Apply" functions. These functions allow for the application of a certain operation to a list of values without the need for a loop. However, looping is still a useful and commonly used method in Mathematica for repetitive calculations or operations.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
248
  • Programming and Computer Science
Replies
4
Views
1K
  • Differential Equations
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
847
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
265
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
4K
  • Special and General Relativity
2
Replies
40
Views
2K
  • Math Proof Training and Practice
2
Replies
61
Views
6K
  • Advanced Physics Homework Help
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
Back
Top