FindRoot - 符号结果转数值

发布于 2024-08-22 15:42:08 字数 680 浏览 3 评论 0原文

我正在使用 FindRoot 语句。我的问题是结果 都只是象征性的形式。如何让mathematica 将结果存储为向量 或列出以方便以后使用。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

許願樹丅啲祈禱 2024-08-29 15:42:08

为了扩展 Michael 的问题,目前的代码不会生成数值,因为 A 和 B 的值尚未设置。

如果为 A 和 B 选择值,FindRoot 不再返回符号答案。

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}

对于你的第二个代码块,我假设你的问题是由 r2[[i]] 引起的:

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

我认为你的意思是:

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

你的意思是使用你在第一个代码块中找到的根吗?如果是这样则需要更换。

r2=r;

r2=r /. roots;

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文