Matematica和Python中的不同解决方案

时间:2016-07-29 17:44:05

标签: python python-3.x wolfram-mathematica solver equation-solving

这是另一个“my-results-not-match-wolfram-website question”。

我最近决定尝试一下python(我不知道为什么,说实话,我花了太多时间研究我不知道是否会使用的东西......我猜想好奇心)。我是一个非常新颖的Python初学者,所以首先,我尝试解决这个等式:

cos(x)*cosh(x)=1

使用python,我编写了下一个代码:

from scipy import optimize 
import numpy as np

func = lambda x : np.cos(x*1.0)*np.cosh(x*1.0)-1.0

for iii in range(1,10):
    solution = fsolve(func, iii*1.0)
    print(solution)

令我惊讶的是,我发现解决方案与wolfram website

不同

基本上,我的解决方案有一些“剩余解决方案”我会说,可能是因为数值错误。

我不知道我做错了什么或遗忘了什么,但代码看起来(在我看来)很好。

任何修复代码的想法都将受到赞赏。

非常感谢所有人。亲切的问候,

德国

=============更新==========

有趣的是,Matlab得到了正确的结果。

NM=5                            ; 
Beta_InitialModal=0             ;
Beta_FinalModal=8*NM            ;

F = @(Beta1) (-1+cos(Beta1)*cosh(Beta1)) ; %Equation of cantilever beam
interval = [Beta_InitialModal, Beta_FinalModal];
N =(Beta_FinalModal-Beta_InitialModal)*50 ;
start_pts = linspace(interval(1),interval(2),N);
found_roots = [];

for iii=1:numel(start_pts)-1
    if length(found_roots)==NM
        break
    else         
        try %#ok<TRYNC>
            found_roots(end+1) = fzero(F,[start_pts(iii),start_pts(iii+1)]); %#ok<SAGROW>
        end
    end 
end
display(found_roots);

Python比Matlab / Mathematica更差吗?我不这么认为......

我想也许这是输入格式编号?像fsolve一样工作的精度低于Matlab?我不太老实。

亲切的问候,

1 个答案:

答案 0 :(得分:0)

不确定你是否意识到,但你的方程有无限多个解,除了零之外,对于大Cos非常接近(n+1/2) Pi或大约n的零。

wolfram alpha页面在没有告诉你的情况下有点误导。

以下是前100个解决方案:(加0)使用具有扩展精度的mathematica:

x /. FindRoot[ Cos[x] Cosh[x] == 1 , {x, (# + 1/2) Pi }, 
 WorkingPrecision -> 1000] & /@ Range[101]
  

4.7300407448627040260,7.8532046240958375565,10.995607838001670907,14.137165491257464177,17.278759657399481438,20.420352245626061091,23.561944902040455075,26.703537555508186248,29.845130209103254267,32.986722862692819562,36.128315516282622650,39.269908169872415463,42.411500823462208720,45.553093477052001958,48.694686130641795196,51.836278784231588435,54.977871437821381673,58.119464091411174912,61.261056745000968150,64.402649398590761388,67.544242052180554627,70.685834705770347865,73.827427359360141104,76.969020012949934342 ,80.110612666539727581,83.252205320129520819,86.393797973719314058,89.535390627309107296,92.676983280898900535,95.818575934488693773,98.960168588078487012,102.10176124166828025,105.24335389525807349,108.38494654884786673,111.52653920243765997,114.66813185602745320,117.80972450961724644,120.95131716320703968,124.09290981679683292,127.23450247038662616,130.37609512397641940,133.51768777756621263,136.65928043115600587,139.8008 7308474579911,142.94246573833559235,146.08405839192538559,149.22565104551517883,152.36724369910497207,155.50883635269476530,158.65042900628455854,161.79202165987435178,164.93361431346414502,168.07520696705393826,171.21679962064373150,174.35839227423352473,177.49998492782331797,180.64157758141311121,183.78317023500290445,186.92476288859269769,190.06635554218249093,193.20794819577228417,196.34954084936207740,199.49113350295187064,202.63272615654166388,205.77431881013145712,208.91591146372125036,212.05750411731104360,215.19909677090083683, 218.34068942449063007,221.48228207808042331,224.62387473167021655,227.76546738526000979,230.90706003884980303,234.04865269243959627,237.19024534602938950,240.33183799961918274,243.47343065320897598,246.61502330679876922,249.75661596038856246,252.89820861397835570,256.03980126756814893,259.18139392115794217,262.32298657474773541,265.46457922833752865,268.60617188192732189,271.74776453551711513,274.889357189106908 37,278.03094984269670160,281.17254249628649484,284.31413514987628808,287.45572780346608132,290.59732045705587456,293.73891311064566780,296.88050576423546103,300.02209841782525427,303.16369107141504751,306.30528372500484075,309.44687637859463399,312.58846903218442723,315.73006168577422047,318.87165433936401370

那些较大的值需要扩展精度来查找..

 Plot[ Cos[x] Cosh[x] - 1 , {x , 310, 320 }]

enter image description here

即使在高精度的情况下注意,如果我们将初始猜测接近零,则FindRoot不会返回精确的零

FindRoot[Cos[x] Cosh[x] == 1, {x, 1/100}]  (* x -> 0.000148132 *)
FindRoot[Cos[x] Cosh[x] == 1, {x, 1/100}, WorkingPrecision -> 100] (* x -> ~3 10^-15 *)

当然如果初始猜测为零FindRoot则返回0 ..

FindRoot[Cos[x] Cosh[x] == 1, {x, 0}] (* x->0. *)