
正文
三次样条函数c语言代码 三次样条插值函数c语言
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
三次样条插值用c语言具体怎么做
void SPL(int n, double *x, double *y, int ni, double *xi, double *yi); 是你所要。
已知 n 个点 x,y; x 必须已按顺序排好。要插值 ni 点,横坐标 xi[], 输出 yi[]。
程序里用double 型,保证计算精度。
SPL调用现成的程序。
现成的程序很多。端点处理方法不同,结果会有不同。想同matlab比较,你需 尝试 调用 spline()函数 时,令 end1 为 1, 设 slope1 的值,令 end2 为 1 设 slope2 的值。
相关问答
Q1: 三次样条插值 C++程序
#includeiostream.h
#includeiomanip.h
#includemath.h
void main()
{
float a[37],b[37];
cout" ""度数"" ""sin(x)值"" ""一阶导值"" ""二阶导值"endl;
for(int i=0;i37;i++)
coutsetw(11)setprecision(3)
10*i
setw(11)setprecision(3)
sin(i*31.4/180)
setw(11)setprecision(3)
cos(i*31.4/180)
setw(11)setprecision(3)
-sin(i*31.4/180)
endl;
cout"一个周期内的积分值:0"endl;
}
Q2: 求助:求三次样条插值函数的C++程序
#includeiostream
#includeiomanip
using
namespace
std;
const
int
MAX
=
50;
float
x[MAX],
y[MAX],
h[MAX];
float
c[MAX],
a[MAX],
fxym[MAX];
float
f(int
x1,
int
x2,
int
x3){
float
a
=
(y[x3]
-
y[x2])
/
(x[x3]
-
x[x2]);
float
b
=
(y[x2]
-
y[x1])
/
(x[x2]
-
x[x1]);
return
(a
-
b)/(x[x3]
-
x[x1]);
}
//求差分
void
cal_m(int
n){
//用追赶法求解出弯矩向量M……
float
B[MAX];
B[0]
=
c[0]
/
2;
for(int
i
=
1;
i
n;
i++)
B[i]
=
c[i]
/
(2
-
a[i]*B[i-1]);
fxym[0]
=
fxym[0]
/
2;
for(i
=
1;
i
=
n;
i++)
fxym[i]
=
(fxym[i]
-
a[i]*fxym[i-1])
/
(2
-
a[i]*B[i-1]);
for(i
=
n-1;
i
=
0;
i--)
fxym[i]
=
fxym[i]
-
B[i]*fxym[i+1];
}
void
printout(int
n);
int
main(){
int
n,i;
char
ch;
do{
cout"Please
put
in
the
number
of
the
dots:";
cinn;
for(i
=
0;
i
=
n;
i++){
cout"Please
put
in
X"i':';
cinx[i];
//coutendl;
cout"Please
put
in
Y"i':';
ciny[i];
//coutendl;
}
for(i
=
0;
i
n;
i++)
//求
步长
h[i]
=
x[i+1]
-
x[i];
cout"Please
输入边界条件\n
1:
已知两端的一阶导数\n
2:两端的二阶导数已知\n
默认:自然边界条件\n";
int
t;
float
f0,
f1;
cint;
switch(t){
case
1:cout"Please
put
in
Y0\'
Y"n"\'\n";
cinf0f1;
c[0]
=
1;
a[n]
=
1;
fxym[0]
=
6*((y[1]
-
y[0])
/
(x[1]
-
x[0])
-
f0)
/
h[0];
fxym[n]
=
6*(f1
-
(y[n]
-
y[n-1])
/
(x[n]
-
x[n-1]))
/
h[n-1];
break;
case
2:cout"Please
put
in
Y0\"
Y"n"\"\n";
cinf0f1;
c[0]
=
a[n]
=
0;
fxym[0]
=
2*f0;
fxym[n]
=
2*f1;
break;
default:cout"不可用\n";//待定
};//switch
for(i
=
1;
i
n;
i++)
fxym[i]
=
6
*
f(i-1,
i,
i+1);
for(i
=
1;
i
n;
i++){
a[i]
=
h[i-1]
/
(h[i]
+
h[i-1]);
c[i]
=
1
-
a[i];
}
a[n]
=
h[n-1]
/
(h[n-1]
+
h[n]);
cal_m(n);
cout"\n输出三次样条插值函数:\n";
printout(n);
cout"Do
you
to
have
anther
try
?
y/n
:";
cinch;
}while(ch
==
'y'
||
ch
==
'Y');
return
0;
}
void
printout(int
n){
coutsetprecision(6);
for(int
i
=
0;
i
n;
i++){
couti+1":
["x[i]"
,
"x[i+1]"]\n""\t";
/*
coutfxym[i]/(6*h[i])"
*
("x[i+1]"
-
x)^3
+
""
*
(x
-
"x[i]")^3
+
"
(y[i]
-
fxym[i]*h[i]*h[i]/6)/h[i]"
*
("x[i+1]"
-
x)
+
"
(y[i+1]
-
fxym[i+1]*h[i]*h[i]/6)/h[i]"(x
-
"x[i]")\n";
coutendl;*/
float
t
=
fxym[i]/(6*h[i]);
if(t
0)coutt"*("x[i+1]"
-
x)^3";
else
cout-t"*(x
-
"x[i+1]")^3";
t
=
fxym[i+1]/(6*h[i]);
if(t
0)cout"
+
"t"*(x
-
"x[i]")^3";
else
cout"
-
"-t"*(x
-
"x[i]")^3";
cout"\n\t";
t
=
(y[i]
-
fxym[i]*h[i]*h[i]/6)/h[i];
if(t
0)cout"+
"t"*("x[i+1]"
-
x)";
else
cout"-
"-t"*("x[i+1]"
-
x)";
t
=
(y[i+1]
-
fxym[i+1]*h[i]*h[i]/6)/h[i];
if(t
0)cout"
+
"t"*(x
-
"x[i]")";
else
cout"
-
"-t"*(x
-
"x[i]")";
coutendlendl;
}
coutendl;
}
Q3: 谁来帮帮我啊:运用三次样条插值函数法对一条已知的折线进行圆滑(编程实现)。
matlab算么?
function output = spline(x,y,xx)
%SPLINE Cubic spline data interpolation.
% PP = SPLINE(X,Y) provides the piecewise polynomial form of the
% cubic spline interpolant to the data values Y at the data sites X,
% for use with the evaluator PPVAL and the spline utility UNMKPP.
% X must be a vector.
% If Y is a vector, then Y(j) is taken as the value to be matched at X(j),
% hence Y must be of the same length as X -- see below for an exception
% to this.
% If Y is a matrix or ND array, then Y(:,...,:,j) is taken as the value to
% be matched at X(j), hence the last dimension of Y must equal length(X) --
% see below for an exception to this.
%
% YY = SPLINE(X,Y,XX) is the same as YY = PPVAL(SPLINE(X,Y),XX), thus
% providing, in YY, the values of the interpolant at XX. For information
% regarding the size of YY see PPVAL.
%
% Ordinarily, the not-a-knot end conditions are used. However, if Y contains
% two more values than X has entries, then the first and last value in Y are
% used as the endslopes for the cubic spline. If Y is a vector, this
% means:
% f(X) = Y(2:end-1), Df(min(X))=Y(1), Df(max(X))=Y(end).
% If Y is a matrix or N-D array with SIZE(Y,N) equal to LENGTH(X)+2, then
% f(X(j)) matches the value Y(:,...,:,j+1) for j=1:LENGTH(X), then
% Df(min(X)) matches Y(:,:,...:,1) and Df(max(X)) matches Y(:,:,...:,end).
%
% Example:
% This generates a sine-like spline curve and samples it over a finer mesh:
% x = 0:10; y = sin(x);
% xx = 0:.25:10;
% yy = spline(x,y,xx);
% plot(x,y,'o',xx,yy)
%
% Example:
% This illustrates the use of clamped or complete spline interpolation where
% end slopes are prescribed. In this example, zero slopes at the ends of an
% interpolant to the values of a certain distribution are enforced:
% x = -4:4; y = [0 .15 1.12 2.36 2.36 1.46 .49 .06 0];
% cs = spline(x,[0 y 0]);
% xx = linspace(-4,4,101);
% plot(x,y,'o',xx,ppval(cs,xx),'-');
%
% Class support for inputs x, y, xx:
% float: double, single
%
% See also INTERP1, PPVAL, UNMKPP, MKPP, SPLINES (The Spline Toolbox).
% Carl de Boor 7-2-86
% Copyright 1984-2004 The MathWorks, Inc.
% $Revision: 5.18.4.3 $ $Date: 2004/03/02 21:48:06 $
output=[];
% Check that data are acceptable and, if not, try to adjust them appropriately
[x,y,sizey,endslopes] = chckxy(x,y);
n = length(x); yd = prod(sizey);
% Generate the cubic spline interpolant in ppform
dd = ones(yd,1); dx = diff(x); divdif = diff(y,[],2)./dx(dd,:);
if n==2
if isempty(endslopes) % the interpolant is a straight line
pp=mkpp(x,[divdif y(:,1)],sizey);
else % the interpolant is the cubic Hermite polynomial
pp = pwch(x,y,endslopes,dx,divdif); pp.dim = sizey;
end
elseif n==3isempty(endslopes) % the interpolant is a parabola
y(:,2:3)=divdif;
y(:,3)=diff(divdif')'/(x(3)-x(1));
y(:,2)=y(:,2)-y(:,3)*dx(1);
pp = mkpp(x([1,3]),y(:,[3 2 1]),sizey);
else % set up the sparse, tridiagonal, linear system b = ?*c for the slopes
b=zeros(yd,n);
b(:,2:n-1)=3*(dx(dd,2:n-1).*divdif(:,1:n-2)+dx(dd,1:n-2).*divdif(:,2:n-1));
if isempty(endslopes)
x31=x(3)-x(1);xn=x(n)-x(n-2);
b(:,1)=((dx(1)+2*x31)*dx(2)*divdif(:,1)+dx(1)^2*divdif(:,2))/x31;
b(:,n)=...
(dx(n-1)^2*divdif(:,n-2)+(2*xn+dx(n-1))*dx(n-2)*divdif(:,n-1))/xn;
else
x31 = 0; xn = 0; b(:,[1 n]) = dx(dd,[2 n-2]).*endslopes;
end
dxt = dx(:);
c = spdiags([ [x31;dxt(1:n-2);0] ...
[dxt(2);2*[dxt(2:n-1)+dxt(1:n-2)];dxt(n-2)] ...
[0;dxt(2:n-1);xn] ],[-1 0 1],n,n);
% sparse linear equation solution for the slopes
mmdflag = spparms('autommd');
spparms('autommd',0);
s=b/c;
spparms('autommd',mmdflag);
% construct piecewise cubic Hermite interpolant
% to values and computed slopes
pp = pwch(x,y,s,dx,divdif); pp.dim = sizey;
end
if nargin==2, output = pp; else output = ppval(pp,xx); end
Q4: 急求二维矩阵三次样条插值的程序,要C或C++语言。望有注释!谢谢! 邮箱lsmann9@hotmail.com
/*********************下面程序是C语言程序(标准C)******************/
/* 计算给定M0,Mn值的三次样条插值多项式 */
/*给定离散点(1.1,0.4),(1.2,0.8),(1.4,1.65),(1.5,1.8),M0=Mn=0,*/
/*用M关系式构造三次样条插值多项式S(x),计算S(1.25)。 */
/*************************************************************/
#include stdio.h
#define Max_N 20
main()
{int i,k,n;
double h[Max_N+1],b[Max_N+1],c[Max_N+1],d[Max_N+1],M[Max_N+1];
double u[Max_N+1],v[Max_N+1],yy[Max_N+1],x[Max_N+1],y[Max_N+1];
double xx,p,q,S;
printf("\nPlease input n value:"); /*输入插值点数n*/
do
{ scanf("%d",n);
if(nMax_N)
printf("\nplease re-input n value:");
}
while(nMax_N||n=0);
printf("Input x[i],i=0,...%d:\n",n-1);
for(i=0;in;i++) scanf("%lf",x[i]);
printf("Input y[i],i=0,...%d:\n",n-1);
for(i=0;in;i++) scanf("%lf",y[i]);
printf("\nInput the M0,Mn value:");
scanf("%lf%lf",M[0],M[n]);
printf("\nInput the x value:"); /*输入计算三次样条插值函数的x值*/
scanf("%lf",xx);
if((xxx[n-1])||(xxx[0]))
{printf("Please input a number between %f and %f.\n",x[0],x[n-1]);
return;
}
/*计算M关系式中各参数的值*/
h[0]=x[1]-x[0];
for(i=1;in;i++)
{h[i]=x[i+1]-x[i];
b[i]=h[i]/(h[i]+h[i-1]);
c[i]=1-b[i];
d[i]=6*((y[i+1]-y[i])/h[i]-(y[i]-y[i-1])/h[i-1])/(h[i]+h[i-1]);
}
/*用追赶法计算Mi,i=1,...,n-1*/
d[1]-=c[1]*M[0];
d[n-1]-=b[n-1]*M[n];
b[n-1]=0; c[1]=0; v[0]=0;
for(i=1;in;i++)
{u[i]=2-c[i]*v[i-1];
v[i]=b[i]/u[i];
yy[i]=(d[i]-c[i]*y[i-1])/u[i];
}
for(i=1;in;i++)
{M[n-i]=yy[n-i]-v[n-i]*M[n-i+1];
}
/*计算三次样条插值函数在x处的值*/
k=0;
while(xx=x[k]) k++;
k=k-1;
p=x[k+1]-xx;
q=xx-x[k];
S=(p*p*p*M[k]+q*q*q*M[k+1])/(6*h[k])+(p*y[k]+q*y[k+1])/h[k]-h[k]*(p*M[k]+q*M[k+1])/6;
printf("S(%f)=%f\n",xx,S); /*输出*/
getch();
}
/*----------------------------------- End of file ------------------------------------*/
/*程序输入输出:
Please input n value:4
Input x[i],i=0,...3:
1.1 1.2 1.4 1.5
Input y[i],i=0,...3:
0.4 0.8 1.65 1.8
Input the M0,Mn value: 0 0
Input the x value:1.25
S(1.250000)=1.033171
*/
Q5: 用vc6.0在mfc中调用opengl如何画三次B样条曲线,求操作步骤和程序代码。谢谢!
#include stdlib.h
#include GL/glut.h
#pragma comment(lib,"glut32.lib")
//
#if 0
// the points of the curve - these are the same as the bezier curve
// points demonstrated in the bezier curve example.
float Points[4][3] = {
{ 10,10,0 },
{ 5,10,2 },
{ -5,0,0 },
{-10,5,-2}
};
#define NUM_POINTS 4
// The following sets of 4 indices are the curves that need to
// be drawn to create a clamped cubic b-spline. In total there
// are 5 curve segments to draw.
//
// 0 0 0 1
// 0 0 1 2
// 0 1 2 3
// 1 2 3 3
// 2 3 3 3
//
// Remember this when trying to understand knot vectors!!
//
#else
float Points[9][3] = {
{ 10,5,0 },
{ 5,10,0 },
{ -5,15,0 },
{ -10,-5,0 },
{ 4,-4,0 },
{ 10,5,0 },
{ 5,10,0 },
{ -5,15,0 },
{ -10,-5,0 }
};
#define NUM_POINTS 9
//若绘制过首尾控制点三次样条函数c语言代码的曲线
// 0 0 0 1
// 0 0 1 2
// 0 1 2 3
// 1 2 3 4
// 2 3 4 5
// 3 4 5 6
// 4 5 6 6
// 5 6 6 6
//
// Remember this when trying to understand knot vectors!!
//
//若绘制首尾相接的平滑曲线 三次样条函数c语言代码,即为当前绘制
// 0 1 2 3
// 1 2 3 4
// 2 3 4 5
// 3 4 5 6
#endif
// the level of detail for the curve
unsigned int LOD=20;
#define NUM_SEGMENTS (NUM_POINTS-3)
//
float* GetPoint(int i)
{
// return 1st point
if (i0)
{
return Points[0];
}
if (iNUM_POINTS)
{
return Points[i];
}
// return last point
return Points[NUM_POINTS-1];
}
//------------------------------------------------------------ OnKeyPress()
void myIdle(void)
{
glutPostRedisplay();
}
//------------------------------------------------------------ OnDraw()
void OnDraw()
{
// clear the screen depth buffer
glClear(GL_COLOR_BUFFER_BIT);
// clear the previous transform
glLoadIdentity();
// set the camera position
// gluLookAt( 1,10,30, // eye pos
// 0,0,0, // aim point
// 0,1,0); // up direction
// glColor3f(0.5,0.2,0);
glPointSize(3);
//
// // draw curve hull
glColor3f(0.3,0,0.5);
glBegin(GL_LINE_STRIP);
for(int i=0;i!=NUM_POINTS;++i)
{
glVertex3fv( Points[i] );
}
glEnd();
glColor3f(0,1,0);
// begin drawing our curve
glBegin(GL_LINE_STRIP);
for(int start_cv=0,j=0;jNUM_SEGMENTS;j++,start_cv++)
{
// for each section of curve, draw LOD number of divisions
for(int i=0;i!=LOD;++i)
{
// use the parametric time value 0 to 1 for this curve
// segment.
float t = (float)i/LOD;
// the t value inverted
float it = 1.0f-t;
// calculate blending functions for cubic bspline
float b0 = it*it*it/6.0f;
float b1 = (3*t*t*t - 6*t*t +4)/6.0f;
float b2 = (-3*t*t*t +3*t*t + 3*t + 1)/6.0f;
float b3 = t*t*t/6.0f;
// calculate the x,y and z of the curve point
float x = b0 * GetPoint( start_cv + 0 )[0] +
b1 * GetPoint( start_cv + 1 )[0] +
b2 * GetPoint( start_cv + 2 )[0] +
b3 * GetPoint( start_cv + 3 )[0] ;
float y = b0 * GetPoint( start_cv + 0 )[1] +
b1 * GetPoint( start_cv + 1 )[1] +
b2 * GetPoint( start_cv + 2 )[1] +
b3 * GetPoint( start_cv + 3 )[1] ;
float z = b0 * GetPoint( start_cv + 0 )[2] +
b1 * GetPoint( start_cv + 1 )[2] +
b2 * GetPoint( start_cv + 2 )[2] +
b3 * GetPoint( start_cv + 3 )[2] ;
// specify the point
glVertex2f( x,y );
}
}
// we need to specify the last point on the curve
//glVertex3fv( Points[NUM_POINTS-1] );
glEnd();
// draw CV's
glBegin(GL_POINTS);
for(int i=0;i!=NUM_POINTS;++i)
{
glVertex3fv( Points[i] );
}
glEnd();
// currently we've been drawing to the back buffer, we need
// to swap the back buffer with the front one to make the image visible
glutSwapBuffers();
}
//------------------------------------------------------------ OnInit()
void OnInit()
{
//glClearColor(1,1,1,0);
}
//------------------------------------------------------------ OnExit()
void OnExit()
{
}
//------------------------------------------------------------ OnReshape()
void OnReshape(int w, int h)
{
// prevents division by zero when minimising window
if (h==0)
{
h=1;
}
// set the drawable region of the window
glViewport(0,0,w,h);
// set up the projection matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// just use a perspective projection
//gluPerspective(45,(float)w/h,0.1,100);
if(w=h)
{
glOrtho(-20.0,20.0,-20.0*(GLfloat)h/(GLfloat)w,20.0*(GLfloat)h/(GLfloat)w,0.0,100.0);
}
else
{
glOrtho(-20.0,20.0,-20.0*(GLfloat)h/(GLfloat)w,20.0*(GLfloat)h/(GLfloat)w,0.0,100.0);
}
// go back to modelview matrix so we can move the objects about
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
//------------------------------------------------------------ main()
int main(int argc,char** argv)
{
// initialise glut
glutInit(argc,argv);
// request a depth buffer, RGBA display mode, and we want double buffering
glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE);
// set the initial window size
glutInitWindowSize(640,480);
// create the window
glutCreateWindow("Clamped B-Spline Curve");
// run our custom initialisation
OnInit();
// set the function to use to draw our scene
glutDisplayFunc(OnDraw);
a href="" target="_blank"涂料加盟/a
// set the function to handle changes in screen size
glutReshapeFunc(OnReshape);
glutIdleFunc(myIdle);
// set the function to be called when we exit
atexit(OnExit);
// this function runs a while loop to keep the program running.
glutMainLoop();
return 0;
}
关于三次样条函数c语言代码和三次样条插值函数c语言的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。








