找回密码
 欢迎注册
查看: 20137|回复: 7

[提问] 谁能把mathematica的计算结果排列优化一下?

[复制链接]
发表于 2014-6-30 08:56:53 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?欢迎注册

×
本帖最后由 cn8888 于 2014-6-30 09:00 编辑

http://bbs.emath.ac.cn/forum.php ... 11&fromuid=8888
问题来自这里.
代码如下
  1. Clear["Global`*"];(*Clear all variables*)
  2. (*把三维空间中的三角形转化成球面a^2+b^2+c^2==1*)
  3. fun=(2*a^2-b^2)(2*b^2-c^2)(2*c^2-a^2)+x*(a^2+b^2+c^2-1)
  4. fa=D[fun,a]
  5. fb=D[fun,b]
  6. fc=D[fun,c]
  7. fx=D[fun,x]
  8. sol1=Solve[{fa==0,fb==0,fc==0,fx==0},{a,b,c,x}]
  9. Print["显示三角形内部的最值"]
  10. sol11=fun/.sol1
  11. Print["数值化"]
  12. N@sol1
  13. N@sol11
复制代码

其中的一行代码如下:
  1. sol1=Solve[{fa==0,fb==0,fc==0,fx==0},{a,b,c,x}]
复制代码

求解结果数值化如下:
{{x -> -0.111111, b -> -0.57735, c -> -0.57735,
  a -> -0.57735}, {x -> -0.111111, b -> -0.57735, c -> -0.57735,
  a -> 0.57735}, {x -> -0.111111, b -> -0.57735, c -> 0.57735,
  a -> -0.57735}, {x -> -0.111111, b -> -0.57735, c -> 0.57735,
  a -> 0.57735}, {x -> -0.111111, b -> 0.57735, c -> -0.57735,
  a -> -0.57735}, {x -> -0.111111, b -> 0.57735, c -> -0.57735,
  a -> 0.57735}, {x -> -0.111111, b -> 0.57735, c -> 0.57735,
  a -> -0.57735}, {x -> -0.111111, b -> 0.57735, c -> 0.57735,
  a -> 0.57735}, {x -> 0., a -> -1., c -> 0., b -> 0.}, {x -> 0.,
  a -> 1., c -> 0., b -> 0.}, {x -> 0., b -> -1., c -> 0.,
  a -> 0.}, {x -> 0., b -> 1., c -> 0., a -> 0.}, {x -> 0.,
  b -> -0.534522, c -> -0.755929, a -> -0.377964}, {x -> 0.,
  b -> -0.534522, c -> -0.755929, a -> 0.377964}, {x -> 0.,
  b -> -0.534522, c -> 0.755929, a -> -0.377964}, {x -> 0.,
  b -> -0.534522, c -> 0.755929, a -> 0.377964}, {x -> 0.,
  b -> 0.534522, c -> -0.755929, a -> -0.377964}, {x -> 0.,
  b -> 0.534522, c -> -0.755929, a -> 0.377964}, {x -> 0.,
  b -> 0.534522, c -> 0.755929, a -> -0.377964}, {x -> 0.,
  b -> 0.534522, c -> 0.755929, a -> 0.377964}, {x -> 0.,
  b -> -0.755929, c -> -0.377964, a -> -0.534522}, {x -> 0.,
  b -> -0.755929, c -> -0.377964, a -> 0.534522}, {x -> 0.,
  b -> -0.755929, c -> 0.377964, a -> -0.534522}, {x -> 0.,
  b -> -0.755929, c -> 0.377964, a -> 0.534522}, {x -> 0.,
  b -> -0.377964, c -> -0.534522, a -> -0.755929}, {x -> 0.,
  b -> -0.377964, c -> -0.534522, a -> 0.755929}, {x -> 0.,
  b -> -0.377964, c -> 0.534522, a -> -0.755929}, {x -> 0.,
  b -> -0.377964, c -> 0.534522, a -> 0.755929}, {x -> 0.,
  b -> 0.377964, c -> -0.534522, a -> -0.755929}, {x -> 0.,
  b -> 0.377964, c -> -0.534522, a -> 0.755929}, {x -> 0.,
  b -> 0.377964, c -> 0.534522, a -> -0.755929}, {x -> 0.,
  b -> 0.377964, c -> 0.534522, a -> 0.755929}, {x -> 0.,
  b -> 0.755929, c -> -0.377964, a -> -0.534522}, {x -> 0.,
  b -> 0.755929, c -> -0.377964, a -> 0.534522}, {x -> 0.,
  b -> 0.755929, c -> 0.377964, a -> -0.534522}, {x -> 0.,
  b -> 0.755929, c -> 0.377964, a -> 0.534522}, {x -> 0., c -> -1.,
  a -> 0., b -> 0.}, {x -> 0., c -> 1., a -> 0.,
  b -> 0.}, {x -> -0.420754, a -> -0.921698, c -> -0.387907,
  b -> 0.}, {x -> -0.420754, a -> -0.921698, c -> 0.387907,
  b -> 0.}, {x -> -0.420754, a -> 0.921698, c -> -0.387907,
  b -> 0.}, {x -> -0.420754, a -> 0.921698, c -> 0.387907,
  b -> 0.}, {x -> -0.420754, b -> -0.387907, c -> -0.921698,
  a -> 0.}, {x -> -0.420754, b -> -0.387907, c -> 0.921698,
  a -> 0.}, {x -> -0.420754, b -> 0.387907, c -> -0.921698,
  a -> 0.}, {x -> -0.420754, b -> 0.387907, c -> 0.921698,
  a -> 0.}, {x -> -0.420754, b -> -0.921698, a -> -0.387907,
  c -> 0.}, {x -> -0.420754, b -> -0.921698, a -> 0.387907,
  c -> 0.}, {x -> -0.420754, b -> 0.921698, a -> -0.387907,
  c -> 0.}, {x -> -0.420754, b -> 0.921698, a -> 0.387907,
  c -> 0.}, {x -> 1.40841, a -> -0.511452, c -> -0.859312,
  b -> 0.}, {x -> 1.40841, a -> -0.511452, c -> 0.859312,
  b -> 0.}, {x -> 1.40841, a -> 0.511452, c -> -0.859312,
  b -> 0.}, {x -> 1.40841, a -> 0.511452, c -> 0.859312,
  b -> 0.}, {x -> 1.40841, b -> -0.511452, a -> -0.859312,
  c -> 0.}, {x -> 1.40841, b -> -0.511452, a -> 0.859312,
  c -> 0.}, {x -> 1.40841, b -> 0.511452, a -> -0.859312,
  c -> 0.}, {x -> 1.40841, b -> 0.511452, a -> 0.859312,
  c -> 0.}, {x -> 1.40841, b -> -0.859312, c -> -0.511452,
  a -> 0.}, {x -> 1.40841, b -> -0.859312, c -> 0.511452,
  a -> 0.}, {x -> 1.40841, b -> 0.859312, c -> -0.511452,
  a -> 0.}, {x -> 1.40841, b -> 0.859312, c -> 0.511452, a -> 0.}}
我明明是按照{a,b,c,x}列举求解变量的.但是....................
求解结果让我很不爽,简直就是胡乱排列有{x,a,c,b}有{x,b,c,a}
请问如何把他们都搞成{a,b,c,x}的?
请问如何让每个求解结果{x -> 1.40841, b -> 0.859312, c -> 0.511452, a -> 0.}一行显示呢?


@hujunhua @wayne @chyanog
你们觉得应该如何弄呢?
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
 楼主| 发表于 2014-6-30 09:09:49 | 显示全部楼层
我的是mathematica 7.0
也许这是mathematica的一个bug
我看solve的帮助里面的结果都是按照指定的变量的顺序排列的,
但是变量只有两个或者三个,而我指定的是4个

点评

@hujunhua  发表于 2014-6-30 09:10
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
发表于 2014-6-30 09:45:20 | 显示全部楼层
这个@wayne的专长,我换电脑了,没装Mathematica 9了,准备等一等直接装Mathematica 10.

看了wayne的一个帖子,说是可以装免费的Mathematica, 还打算学一学呢。Linux、树莓派、Free Mathematica,一连串的,得花时间折腾。

评分

参与人数 1威望 +12 金币 +12 贡献 +12 经验 +12 鲜花 +12 收起 理由
wayne + 12 + 12 + 12 + 12 + 12 赞助一下!

查看全部评分

毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
发表于 2014-6-30 14:29:55 | 显示全部楼层
或许可以用下面的命令试试。
ans = {{x -> 1, a -> 2, b -> 3}, {b -> 2, a -> 3, x -> 1}};
Map[First[#] -> Last[#] &, Map[Transpose[{{x, a, b}, #}] &, {x, a, b} /. ans], {2}]
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
发表于 2014-6-30 14:44:24 | 显示全部楼层
  1. {a -> (a /. #), b -> (b /. #), c -> (c /. #), x -> (x /. #)} & /@%
复制代码


毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
发表于 2014-6-30 14:47:49 | 显示全部楼层
上面是指定次序,如果是默认字母排序可以
  1. Map[Sort, %]
复制代码

毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
发表于 2014-7-3 23:13:54 | 显示全部楼层
  1. fun = (2*a^2 - b^2) (2*b^2 - c^2) (2*c^2 - a^2) +  x*(a^2 + b^2 + c^2 - 1);
  2. Column[N@Solve[# == 0 & /@ (D[fun, #] & /@ #), #] &@{a, b, c, x}]
复制代码
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
您需要登录后才可以回帖 登录 | 欢迎注册

本版积分规则

小黑屋|手机版|数学研发网 ( 苏ICP备07505100号 )

GMT+8, 2024-11-22 01:08 , Processed in 0.029947 second(s), 18 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表