C++曲线拟合代码
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++曲线拟合代码相关的知识,希望对你有一定的参考价值。
请各位大侠帮帮忙啦!
参考技术A 用最小二乘法#include "stdio.h"
#include "math.h"
#define num 10
float neiji(float b[num],float c[num]) //内积函数
int p;
float nj=0;
for (p=1;p<num;p++)
nj+=c[p]*b[p];
return nj;
float s[num],x[num],y[num],fai[num][num],afa[num],beida[num],a[num],xfai[num],yd[num],max,pcpfh;
void main()
int i,j,k,n,index,flag;
char conti;
conti=' ';
printf("请输入已知点的个数n=\n");
scanf("%d",&n);
printf("请输入x和y:");
for(i=1;i<=n;i++)
printf("x[%d]=",i);
scanf("%f",&x[i]);
printf("y[%d]=",i);
scanf("%f",&y[i]);
while(conti==' ')
printf("请输入拟和次数=");
scanf("%d",&index);
pcpfh=0;
afa[1]=0;
a[0]=0;
for(i=1;i<=n;i++)
afa[1]+=x[i];
a[0]+=y[i];
fai[0][i]=1;
afa[1]=afa[1]/n;
a[0]=a[0]/n;
for (i=1;i<=n;i++)
fai[1][i]=x[i]-afa[1];
a[1]=neiji(fai[1],y)/neiji(fai[1],fai[1]);
for(k=1;k<index;k++)
for(i=1;i<=n;i++)
xfai[i]=x[i]*fai[k][i];
afa[k+1]=neiji(fai[k],xfai)/neiji(fai[k],fai[k]);
beida[k]=neiji(fai[k],fai[k])/neiji(fai[k-1],fai[k-1]);
for(j=1;j<=n;j++)
fai[k+1][j]=(x[j]-afa[k+1])*fai[k][j]-beida[k]*fai[k-1][j];
a[k+1]=neiji(fai[k+1],y)/neiji(fai[k+1],fai[k+1]);
printf("%d次拟和结果为\n",index);
for(i=0;i<=index;i++)
printf("a[%d]=%f\n",i,a[i]);
for(i=1;i<=index;i++)
printf("afa[%d]=%f\n",i,afa[i]);
for(i=1;i<index;i++)
printf("beida[%d]=%f\n",i,beida[i]);
for(i=1;i<=n;i++)
for(k=0;k<=index;k++)
s[i]+=a[k]*fai[k][i];
yd[i]=float(fabs(y[i]-s[i]));
pcpfh+=yd[i]*yd[i];
s[i]=0;
max=0;
for(i=1;i<=n;i++)
if(yd[i]>max)
max=yd[i];
flag=i;
printf("当x=%f时,偏差最大=%f,偏差平方和为%f\n",x[flag],max,pcpfh);
printf("继续拟和请按space,按其他键退出");
conti=getchar();
conti=getchar();
参考资料:csdn网站上的
本回答被提问者采纳 参考技术B 太难了tensorflow 曲线拟合
tensorflow 曲线拟合
Python代码:
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
# from tensorflow.examples.tutorials.mnist import input_data
# creating data
mu,sigma=0, 0.1
data_size = 300
x_data = np.linspace(-1, 1,data_size)[:, np.newaxis]
# noise = np.random.normal(0,0.05, x_data.shape)
y_data = np.sign(x_data)
# mnist data
# mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
# x_data, y_data = mnist.train.next_batch(300)
# input layer
xs = tf.placeholder(tf.float32, [None, 1])
ys = tf.placeholder(tf.float32, [None, 1])
# layer function
def layer(data_in, size, func = None):
w = tf.Variable(tf.random_normal(size))
b = tf.Variable(tf.zeros([1, size[1]]))
z = tf.matmul(data_in, w) + b
if(func):
data_out = func(z)
else:
data_out = z
return data_out
# hidden layer
output1 = layer(xs, [1, 10], tf.nn.relu)
output2 = layer(output1, [10, 20], tf.nn.softmax)
output3 = layer(output2, [20, 20], tf.nn.relu)
output4 = layer(output3, [20, 10], tf.nn.softmax)
output5 = layer(output4, [10, 10], tf.nn.relu)
# output layer
out = layer(output5, [10, 1])
# loss function
# loss = tf.reduce_sum(ys * tf.log(out))
loss = tf.reduce_mean(tf.reduce_sum(tf.square((out - ys))))
# trainning method
# train = tf.train.GradientDescentOptimizer(0.1).minimize(loss)
train = tf.train.AdamOptimizer().minimize(loss)
# init all variables
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
# print loss value for every 50 times loop
print_step = 50
# loop less than 50 * 1000
loop_max_count = 1000
while True:
print_step -= 1
_,loss_value = sess.run([train,loss],feed_dict={xs:x_data,ys:y_data})
if(print_step == 0):
print(loss_value)
print_step = 50
loop_max_count -= 1
if(loss_value < .00001 or loop_max_count <= 0):
break
# print loop times and show the output
print("loop_count = ", (1000 - loop_max_count) * 50)
y_out = sess.run(out, feed_dict={xs:x_data})
plt.plot(x_data, y_out, label="out")
plt.plot(x_data, y_data, label="in")
plt.show()
可以用来看看不同数目的隐含层和不同的激活函数对曲线拟合的训练性能和训练结果有何影响。
以上是关于C++曲线拟合代码的主要内容,如果未能解决你的问题,请参考以下文章