I will be very happy if you could help me to solve the following problem.
The simplified version of my problem is the following.
I have a quadratic equation with a parameter, say "p".
Define the function:
f[x_] := A*X^2 + B*X*p + D with A, B and D are given.
"p" is a parameter.
I solve this equation for a given p:
w = Solve[f[x]] == 0, X].
Now I want to make a Table for the solutions of this
quadratic equation for different values of "p".
So I write,
Table[{p,sol},{p,0,10,1}.
I will get a table which will look like,
0 {w -> a} {w -> -a}
1 {w -> b} {w -> -b}
2 {w -> c} {w -> -c}
and so on.........
How can I remove the curly brackets and the arrow symbol
from the TableForm? I would like to plot the 1st column
vs. 2nd or 3rd column. I must remove those brackets.
and I wish to get the following form:
0 a -a
1 b -b
2 c -c
Hope this can be done easily which I am not aware.
Any suggestion/solution to this problem is most welcome.
Thanking you in advance.
TKG.
First please note that 'x' and 'X' are distinct variables. If you give a
quadratic equation to Solve, you will get back a list of TWO solutions -
because that is the nature of quadratic equations! If you don't care
which solution you want, you could just select one of these:
w=Solve[f[x] == 0, x][[1]]
At this point w will have a value something like {x->answer}. This is
extremely useful in general because you can use it to substitute the
value of x into any expression involving x. In your case you want:
w= x/.Solve[f[x] == 0, x][[1]]
Now w is the answer you want (as a function of p) and you can use it to
build your table without any curly brackets!
If I were you I would invest some time reading that heavy brick that
came with your Mathematica disks - or even go on a course - once you
learn a little more you will see how all this clicks together.
David Bailey
http://www.dbaileyconsultancy.co.uk
w=x/.Solve[f[x]==0,x];
Table[Prepend[w,p],{p,0,10}]//TableForm
Bob Hanlon
f[x_] := x^2 + 2*x*p + 3
w = x /. Solve[f[x] == 0, x]
PrependTo[w, p]
Table[w /. p -> x, {x, 2, 5}] // TableForm
sincerely, Daniel
>Hi Math Guru,
>
>I will be very happy if you could help me to solve the following problem.
>The simplified version of my problem is the following.
>I have a quadratic equation with a parameter, say "p".
>
>Define the function:
>f[x_] := A*X^2 + B*X*p + D with A, B and D are given.
>"p" is a parameter.
>
>
First things first, using D as a variable will not work as it is a
protected symbol. Try it by typing ??D . Infact using capital letters to
define your variables is not a good idea (it is my opinion ofcourse)
>I solve this equation for a given p:
>w = Solve[f[x]] == 0, X].
>
>
You mean Solve[f[x]==0,x]
f[x_] := a*x^2 + b*x*p + d
The way I have learned to do this is
sol1=Solve[f[x]==0,x]
w1[a_,b_,d_,p_]=x/.sol1[[1]]
w2[a_,b_,d_,p_]=x/.sol1[[2]]
This will give you a function w1 and w2 in terms of your parameters
without the curly brackets
>Now I want to make a Table for the solutions of this
>quadratic equation for different values of "p".
>So I write,
>Table[{p,sol},{p,0,10,1}.
>
>
dat=Table[{p,w1[a,b,d,p],w2[a,b,d,p]},{p,0,10,1}]
Here you can input your values for a,b &d etc p is iterated in your table
>I will get a table which will look like,
>0 {w -> a} {w -> -a}
>1 {w -> b} {w -> -b}
>2 {w -> c} {w -> -c}
>and so on.........
>
>
>How can I remove the curly brackets and the arrow symbol
>from the TableForm? I would like to plot the 1st column
>vs. 2nd or 3rd column.
>
This is a little tricky, maybe some one can provide a simpler way to do
this. I have used the MatrixManipulation package to get plot the first
columns vs the other two, note if you have complex solution you have to
modify your plot commands. You may also try the multiple list package
dat = Table[{p, w1[1, 1,0, p], w1[1, 1, 0, p]}, {p, 0, 10, 1}]
dat // MatrixForm
<< LinearAlgebra`MatrixManipulation`
ListPlot[TakeColumns[dat, 2]]
ListPlot[Table[{Flatten[TakeColumns[dat, 1]][[b]],
Flatten[TakeColumns[dat, \
-1]][[b]]}, {b, 1, Length[dat]}]]
> I must remove those brackets.
>and I wish to get the following form:
>0 a -a
>1 b -b
>2 c -c
>
>
>Hope this can be done easily which I am not aware.
>Any suggestion/solution to this problem is most welcome.
>Thanking you in advance.
>TKG.
>
>
>
>
Hope this helps
Best regards
Pratik
--
Pratik Desai
Graduate Student
UMBC
Department of Mechanical Engineering
Phone: 410 455 8134
TableForm@% yields
I am not sure that I have fully understood what you wanted; however, I
hope that the following lines will help you.
First we define the function f and solve the equation for the variable X
In[1]:=
f[x_] := A*X^2 + B*X*p + D
In[2]:=
w = Solve[f[x] == 0, X]
Out[2]=
{{X -> ((-B)*p - Sqrt[-4*A*D + B^2*p^2])/(2*A)},
{X -> ((-B)*p + Sqrt[-4*A*D + B^2*p^2])/(2*A)}}
Since we have not defined any values for A, B and D yet, we get symbolic
solutions that we use in the next line to compute the table.
In[3]:=
sol = Table[w, {p, 0, 10}]
Out[3]=
{{{X -> -(Sqrt[(-A)*D]/A)}, {X -> Sqrt[(-A)*D]/A}},
{{X -> (-B - Sqrt[B^2 - 4*A*D])/(2*A)},
{X -> (-B + Sqrt[B^2 - 4*A*D])/(2*A)}},
{{X -> (-2*B - Sqrt[4*B^2 - 4*A*D])/(2*A)},
{X -> (-2*B + Sqrt[4*B^2 - 4*A*D])/(2*A)}},
{{X -> (-3*B - Sqrt[9*B^2 - 4*A*D])/(2*A)},
{X -> (-3*B + Sqrt[9*B^2 - 4*A*D])/(2*A)}},
{{X -> (-4*B - Sqrt[16*B^2 - 4*A*D])/(2*A)},
{X -> (-4*B + Sqrt[16*B^2 - 4*A*D])/(2*A)}},
{{X -> (-5*B - Sqrt[25*B^2 - 4*A*D])/(2*A)},
{X -> (-5*B + Sqrt[25*B^2 - 4*A*D])/(2*A)}},
{{X -> (-6*B - Sqrt[36*B^2 - 4*A*D])/(2*A)},
{X -> (-6*B + Sqrt[36*B^2 - 4*A*D])/(2*A)}},
{{X -> (-7*B - Sqrt[49*B^2 - 4*A*D])/(2*A)},
{X -> (-7*B + Sqrt[49*B^2 - 4*A*D])/(2*A)}},
{{X -> (-8*B - Sqrt[64*B^2 - 4*A*D])/(2*A)},
{X -> (-8*B + Sqrt[64*B^2 - 4*A*D])/(2*A)}},
{{X -> (-9*B - Sqrt[81*B^2 - 4*A*D])/(2*A)},
{X -> (-9*B + Sqrt[81*B^2 - 4*A*D])/(2*A)}},
{{X -> (-10*B - Sqrt[100*B^2 - 4*A*D])/(2*A)},
{X -> (-10*B + Sqrt[100*B^2 - 4*A*D])/(2*A)}}}
Say that the values of A, B and D are 2, 3 and 5, respectively.
In[4]:=
sol = sol /. {A -> 2, B -> 3, D -> 5}
Out[4]=
{{{X -> (-I)*Sqrt[5/2]}, {X -> I*Sqrt[5/2]}},
{{X -> (1/4)*(-3 - I*Sqrt[31])},
{X -> (1/4)*(-3 + I*Sqrt[31])}},
{{X -> -(3/2) - I/2}, {X -> -(3/2) + I/2}},
{{X -> (1/4)*(-9 - Sqrt[41])},
{X -> (1/4)*(-9 + Sqrt[41])}},
{{X -> (1/4)*(-12 - 2*Sqrt[26])},
{X -> (1/4)*(-12 + 2*Sqrt[26])}},
{{X -> (1/4)*(-15 - Sqrt[185])},
{X -> (1/4)*(-15 + Sqrt[185])}},
{{X -> (1/4)*(-18 - 2*Sqrt[71])},
{X -> (1/4)*(-18 + 2*Sqrt[71])}},
{{X -> (1/4)*(-21 - Sqrt[401])},
{X -> (1/4)*(-21 + Sqrt[401])}},
{{X -> (1/4)*(-24 - 2*Sqrt[134])},
{X -> (1/4)*(-24 + 2*Sqrt[134])}},
{{X -> (1/4)*(-27 - Sqrt[689])},
{X -> (1/4)*(-27 + Sqrt[689])}},
{{X -> (1/4)*(-30 - 2*Sqrt[215])},
{X -> (1/4)*(-30 + 2*Sqrt[215])}}}
Now we have numerical solutions expressed as _replacement rules_. To get
an array of numbers only, we use these rules to replace X by its values
in sol.
In[5]:=
sol = X /. sol
Out[5]=
{{(-I)*Sqrt[5/2], I*Sqrt[5/2]},
{(1/4)*(-3 - I*Sqrt[31]), (1/4)*(-3 + I*Sqrt[31])},
{-(3/2) - I/2, -(3/2) + I/2},
{(1/4)*(-9 - Sqrt[41]), (1/4)*(-9 + Sqrt[41])},
{(1/4)*(-12 - 2*Sqrt[26]),
(1/4)*(-12 + 2*Sqrt[26])},
{(1/4)*(-15 - Sqrt[185]), (1/4)*(-15 + Sqrt[185])},
{(1/4)*(-18 - 2*Sqrt[71]),
(1/4)*(-18 + 2*Sqrt[71])},
{(1/4)*(-21 - Sqrt[401]), (1/4)*(-21 + Sqrt[401])},
{(1/4)*(-24 - 2*Sqrt[134]),
(1/4)*(-24 + 2*Sqrt[134])},
{(1/4)*(-27 - Sqrt[689]), (1/4)*(-27 + Sqrt[689])},
{(1/4)*(-30 - 2*Sqrt[215]),
(1/4)*(-30 + 2*Sqrt[215])}}
Then we transpose the matrix sol to plot easily one or the other column.
In[6]:=
sol = Transpose[sol]
Out[6]=
{{(-I)*Sqrt[5/2], (1/4)*(-3 - I*Sqrt[31]),
-(3/2) - I/2, (1/4)*(-9 - Sqrt[41]),
(1/4)*(-12 - 2*Sqrt[26]), (1/4)*(-15 - Sqrt[185]),
(1/4)*(-18 - 2*Sqrt[71]), (1/4)*(-21 - Sqrt[401]),
(1/4)*(-24 - 2*Sqrt[134]), (1/4)*(-27 - Sqrt[689]),
(1/4)*(-30 - 2*Sqrt[215])}, {I*Sqrt[5/2],
(1/4)*(-3 + I*Sqrt[31]), -(3/2) + I/2,
(1/4)*(-9 + Sqrt[41]), (1/4)*(-12 + 2*Sqrt[26]),
(1/4)*(-15 + Sqrt[185]), (1/4)*(-18 + 2*Sqrt[71]),
(1/4)*(-21 + Sqrt[401]), (1/4)*(-24 + 2*Sqrt[134]),
(1/4)*(-27 + Sqrt[689]), (1/4)*(-30 + 2*Sqrt[215])}}
Line 7 plots the first column with an index for the x-axes starting from
1 (so ranging from 1 to 11 in our case). Line 8 plots the same thing but
with a range from 0 to 10 on the x-axes.
In[7]:=
ListPlot[sol[[1]]];
In[8]:=
ListPlot[Transpose[{Range[0, 10], sol[[1]]}]];
Finally, we construct a nice table with 3 columns.
In[9]:=
TableForm[Transpose[{Range[0, 10], sol[[1]],
sol[[2]]}]]
Out[9]//TableForm=
TableForm[{{0, (-I)*Sqrt[5/2], I*Sqrt[5/2]},
{1, (1/4)*(-3 - I*Sqrt[31]),
(1/4)*(-3 + I*Sqrt[31])}, {2, -(3/2) - I/2,
-(3/2) + I/2}, {3, (1/4)*(-9 - Sqrt[41]),
(1/4)*(-9 + Sqrt[41])},
{4, (1/4)*(-12 - 2*Sqrt[26]),
(1/4)*(-12 + 2*Sqrt[26])},
{5, (1/4)*(-15 - Sqrt[185]),
(1/4)*(-15 + Sqrt[185])},
{6, (1/4)*(-18 - 2*Sqrt[71]),
(1/4)*(-18 + 2*Sqrt[71])},
{7, (1/4)*(-21 - Sqrt[401]),
(1/4)*(-21 + Sqrt[401])},
{8, (1/4)*(-24 - 2*Sqrt[134]),
(1/4)*(-24 + 2*Sqrt[134])},
{9, (1/4)*(-27 - Sqrt[689]),
(1/4)*(-27 + Sqrt[689])},
{10, (1/4)*(-30 - 2*Sqrt[215]),
(1/4)*(-30 + 2*Sqrt[215])}}]
Best regards,
/J.M.