Citar
#include<cstdlib>
#include<iostream>
#include<cstdio>
using namespace std;
class interpolacion
{
private:
float x,x0,x1,r,d,fx0,fx1,fx;
public:
void leer();
float calcular();
};
void interpolacion::leer()
{
do{
cout<<"\nValor de x al cual se quiere aproximar:\t";
cin>>x;}while(x<1 || x>1492);
}
float interpolacion::calcular()
{
int i;
float Y[] = {0,0.010373807,
-0.05124284,
-0.227092782,
-0.572418858,
-1.150211522,
-2.019461229,
-3.247663205,
-4.904050745,
-7.062119076,
-9.803353877,
-13.21724083,
-17.39877039,
-22.45717585,
-28.51818573,
-35.71928571,
-44.22644716,
-54.22539859,
-65.94810183,
-79.66102345,
-95.70661095,
-114.4980595,
-136.5895354,
-162.693691,
-193.8151306,
-231.3914014,
-277.6754313,
-336.5191712,
-415.6610186,
-536.5034235,
-763.8268297,
-804.7677106},
X[] = {0,50.002,
99.999,
149.999,
199.997,
249.997,
300.007,
349.993,
400.003,
449.997,
499.994,
550.005,
600.002,
649.993,
700.003,
749.995,
800.004,
849.995,
900.004,
949.999,
1000.006,
1049.997,
1100.004,
1150.001,
1200.005,
1250.002,
1300,
1349.999,
1399.998,
1449.998,
1490,
1492};
/*aproximamos el valor a calcular a los puntos menor y mayor el en vector X
del punto que queremos calcular*/
for(i=0;x>X;i++){
x0=X;
fx0=Y;
x1=X[i+1];
fx1=Y[i+1];
}
//Metodo de interpolacion:
r=fx1-fx0;
d=r/(x1-x0);
fx=fx0+(d*(x-x0));
return fx;
}
int main()
{
float a;
interpolacion obj;
obj.leer();
cout<<"\nEl valor de f(x)es:" << obj.calcular() << endl;
//getchar();
}
Mi avance de Codigo en Python es el siguiente:#include<iostream>
#include<cstdio>
using namespace std;
class interpolacion
{
private:
float x,x0,x1,r,d,fx0,fx1,fx;
public:
void leer();
float calcular();
};
void interpolacion::leer()
{
do{
cout<<"\nValor de x al cual se quiere aproximar:\t";
cin>>x;}while(x<1 || x>1492);
}
float interpolacion::calcular()
{
int i;
float Y[] = {0,0.010373807,
-0.05124284,
-0.227092782,
-0.572418858,
-1.150211522,
-2.019461229,
-3.247663205,
-4.904050745,
-7.062119076,
-9.803353877,
-13.21724083,
-17.39877039,
-22.45717585,
-28.51818573,
-35.71928571,
-44.22644716,
-54.22539859,
-65.94810183,
-79.66102345,
-95.70661095,
-114.4980595,
-136.5895354,
-162.693691,
-193.8151306,
-231.3914014,
-277.6754313,
-336.5191712,
-415.6610186,
-536.5034235,
-763.8268297,
-804.7677106},
X[] = {0,50.002,
99.999,
149.999,
199.997,
249.997,
300.007,
349.993,
400.003,
449.997,
499.994,
550.005,
600.002,
649.993,
700.003,
749.995,
800.004,
849.995,
900.004,
949.999,
1000.006,
1049.997,
1100.004,
1150.001,
1200.005,
1250.002,
1300,
1349.999,
1399.998,
1449.998,
1490,
1492};
/*aproximamos el valor a calcular a los puntos menor y mayor el en vector X
del punto que queremos calcular*/
for(i=0;x>X;i++){
x0=X;
fx0=Y;
x1=X[i+1];
fx1=Y[i+1];
}
//Metodo de interpolacion:
r=fx1-fx0;
d=r/(x1-x0);
fx=fx0+(d*(x-x0));
return fx;
}
int main()
{
float a;
interpolacion obj;
obj.leer();
cout<<"\nEl valor de f(x)es:" << obj.calcular() << endl;
//getchar();
}
Citar
'''
Modulo Movimiento Nanometros
@author: P1R0
'''
def metodo(x):
#float y, x0, y0, x1, y1;
Y = [0,
0.010373807,
-0.05124284,
-0.227092782,
-0.572418858,
-1.150211522,
-2.019461229,
-3.247663205,
-4.904050745,
-7.062119076,
-9.803353877,
-13.21724083,
-17.39877039,
-22.45717585,
-28.51818573,
-35.71928571,
-44.22644716,
-54.22539859,
-65.94810183,
-79.66102345,
-95.70661095,
-114.4980595,
-136.5895354,
-162.693691,
-193.8151306,
-231.3914014,
-277.6754313,
-336.5191712,
-415.6610186,
-536.5034235,
-763.8268297,
-804.7677106];
X = [0,
50.002,
99.999,
149.999,
199.997,
249.997,
300.007,
349.993,
400.003,
449.997,
499.994,
550.005,
600.002,
649.993,
700.003,
749.995,
800.004,
849.995,
900.004,
949.999,
1000.006,
1049.997,
1100.004,
1150.001,
1200.005,
1250.002,
1300,
1349.999,
1399.998,
1449.998,
1490,
1492];
#x = float(x);
for i in X:
if x > X:
x0=X;
y0=Y;
x1=X[i+1];
y1=Y[i+1];
else:
break
r=y1-y0;
d=r/(x1-x0);
y=y0+(d*(x-x0));
return y
N = raw_input( "Ingresa Nanometros:");
N = float(N);
print "el error es de %f" % metodo(N);
Modulo Movimiento Nanometros
@author: P1R0
'''
def metodo(x):
#float y, x0, y0, x1, y1;
Y = [0,
0.010373807,
-0.05124284,
-0.227092782,
-0.572418858,
-1.150211522,
-2.019461229,
-3.247663205,
-4.904050745,
-7.062119076,
-9.803353877,
-13.21724083,
-17.39877039,
-22.45717585,
-28.51818573,
-35.71928571,
-44.22644716,
-54.22539859,
-65.94810183,
-79.66102345,
-95.70661095,
-114.4980595,
-136.5895354,
-162.693691,
-193.8151306,
-231.3914014,
-277.6754313,
-336.5191712,
-415.6610186,
-536.5034235,
-763.8268297,
-804.7677106];
X = [0,
50.002,
99.999,
149.999,
199.997,
249.997,
300.007,
349.993,
400.003,
449.997,
499.994,
550.005,
600.002,
649.993,
700.003,
749.995,
800.004,
849.995,
900.004,
949.999,
1000.006,
1049.997,
1100.004,
1150.001,
1200.005,
1250.002,
1300,
1349.999,
1399.998,
1449.998,
1490,
1492];
#x = float(x);
for i in X:
if x > X:
x0=X;
y0=Y;
x1=X[i+1];
y1=Y[i+1];
else:
break
r=y1-y0;
d=r/(x1-x0);
y=y0+(d*(x-x0));
return y
N = raw_input( "Ingresa Nanometros:");
N = float(N);
print "el error es de %f" % metodo(N);
Es Obvio que el problema esta en el uso del FOR que aun no logro comprender del todo.. si alguien me pudiera facilitar esta parte se lo agradeceria mucho... básicamente estoy desarrollando un software para manejar un monocromador en python con un poco de ingenieria inversa... de esta forma hago aproximaciones para disminuir un error del motor respecto a lo que me da el software del fabricante pero esa es otra historia :p de antemano les agradezco!