0
votes

I am using the FindRoot statement. My problem is that the results are only in symbolic form. How can I get mathematica to store results as a vector or list for easy use later.

g[r_]:=(A^r - 1)/(A^r - B^r);
func[r_]:= Piecewise[{{g[r],r<-.01 },{ g[r],r>.01} }];
roots = Table[0,{10}];
q= Table[pp,{pp,.01,0.1,0.01}];
Do[ roots[[i]]=FindRoot[func[r]== q[[i]],{r,0.9}];,{i,1,10}];    ********
Print[r/.roots];           *********** this prints out ok as a list


Pa2=Table[0,{10}];
myPa2=Table[0,{10}];
i/:IntegerQ[i]=True;    
r2=r;
h[r2_]:=(A^r2 - 1)/(A^r2 - B^r2);
funcOC[r2_]:= Piecewise[{{h[r2],r2<-.01 },{ h[r2],r2>.01} }];  
Do[ Pa2[[i]]=funcOC[r2[[i]]], {i,1,10} ];
Print[myPa2/.Pa2];      ****************symbolic notation is output
1
Can you give a set of (small) example values for A, B, and r please? - Michael Pilat
There are a few pieces of your code that do not seem to serve a purpose. i/:IntegerQ[i]=True; and r2=r; and r2[[i]]. Are you new to Mathematica, do you have a background in Matlab or FORTRAN? - Davorak
I have been using Mathematica for at least 10 years now. Programming in Mathematica has not been going well for me and I was determined to learn how to do it. I like to work with modules - Mary A. Marion

1 Answers

1
votes

To expand on Michael's question the code, as it currently stands, does not produce numerical values because the values of A and B have not been set.

If values are chosen for A and B FindRoot no longer returns a symbolic answer.

g[r_] := (A^r - 1)/(A^r - B^r);
A = .4; B = .5;
func[r_] := Piecewise[{{g[r], r < -.01}, {g[r], r > .01}}];
roots = Table[0, {10}];
q = Table[pp, {pp, .01, 0.1, 0.01}];
Do[roots[[i]] = FindRoot[func[r] == q[[i]], {r, 0.9}];, {i, 1, 10}];
Print[r /. roots];

{-201.021,-198.983,-196.97,-194.98,-186.987,-178.398,-170.282,-162.61,-155.352,-148.484}

For your second block of code I assume your problem is caused by r2[[i]] in:

Do[ Pa2[[i]]=funcOC[r2[[i]]], {i,1,10} ];

I think you meant:

Do[ Pa2[[i]]=funcOC[i], {i,1,10} ];

Did you mean to use the roots you found in your first block of code? If so you need to replace.

r2=r;

with

r2=r /. roots;