Xu_PY
2021-02-06 22:30
采纳率: 66.7%
浏览 851

如何matlab或者python实现多元函数的非线性拟合。

比如z=f(x,y),给出(x,y,z)的多个数据点,然后怎么拟合出f函数,给出源码就更好了

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • 歇歇 2021-02-06 22:51
    已采纳

    matlab

    x1 = [1 2 3 4 5 6]';

    x2 = [.2 .4 .8 .9 1.1 2.1]';

    X = [x1 x2]; 

    f = [ 3.3079 6.6358 10.3143 13.6492 17.2755 23.6271]';

    objfcn = @(A,X)A(1)*x1 + x2.^(A(2));

    x0=1; 

    x0=(1)*[1;1];

    [vestimated,resnorm] = lsqcurvefit(objfcn,x0,X,f)

    点赞 2 评论
  • Wayne帆 2021-02-06 22:41

    Python多项式拟合:

    import matplotlib.pyplot as plt
    import numpy as np
    
    x = np.arange(1, 22, 1)
    y = np.array([176.55823623919866, 175.82629227599108, 174.6682288122686, 174.1676016833001, 173.18851142144007, 171.50626507572986, 171.30220775004557, 171.36967661238003, 171.28415302420305, 171.28824729828153, 171.57034121168718, 172.33094464950744, 173.23238914855358, 173.55613816498442, 174.3453315452684, 175.8405706931801, 176.55279402689868, 176.61370282973544, 176.81582616485196, 177.02915557477107, 176.8786448208988])
    z1 = np.polyfit(x, y, 100) # 用3次多项式拟合
    p1 = np.poly1d(z1)
    print(p1) # 在屏幕上打印拟合多项式
    yvals=p1(x) # 也可以使用yvals=np.polyval(z1,x)
    plot1=plt.plot(x, y, '*',label='original values')
    plot2=plt.plot(x, yvals, 'r',label='polyfit values')
    plt.xlabel('x axis')
    plt.ylabel('y axis')
    plt.legend(loc=4) # 指定legend的位置,读者可以自己help它的用法
    plt.title('polyfitting')
    plt.show()
    plt.savefig('p1.png')
    

    https://blog.csdn.net/wayne6515/article/details/105004558

    点赞 2 评论

相关推荐 更多相似问题