-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinspace.cpp
executable file
·81 lines (72 loc) · 1.91 KB
/
Linspace.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/////////////////////////////////////////////////////////////////////////
// Program : linspace.cpp
// Coded by : Prof. Gang-Gyoo Jin, Korea Maritime University
// Coded on : 12/12/2003
#include <math.h>
#include "rmatrix.h"
/////////////////////////////////////////////////////////////////////////
// generates n equally spaced points between x1 and x2.
void ms_LinSpace(REAL x1, REAL x2, int n, REAL x[])
{
REAL step;
if(n < 2)
ErrorMsg("Error in 'LinSpace': use n >= 2");
step= (x2-x1)/REAL(n-1);
x[0]= x1;
for(int i=1; i<n-1; i++)
x[i]= x[i-1]+step;
x[n-1]= x2;
return;
}
/////////////////////////////////////////////////////////////////////////
// generates n equally spaced points between x1 and x2.
RVECTOR ms_LinSpace(REAL x1, REAL x2, int n)
{
REAL step;
RVECTOR x(n);
if(n < 2)
ErrorMsg("Error in 'LinSpace': use n >= 2");
step= (x2-x1)/REAL(n-1);
x(0)= x1;
for(int i=1; i<n-1; i++)
x(i)= x(i-1)+step;
x(n-1)= x2;
return x;
}
/////////////////////////////////////////////////////////////////////////
// generates n logarithmically equally spaced points between
// decades 10^d1 and 10^d2
void ms_LogSpace(int d1, int d2, int n, REAL x[])
{
int i;
REAL step;
if(n < 2)
ErrorMsg("Error in 'LinSpace': use n >= 2");
step= (REAL)(d2-d1)/(REAL)(n-1);
x[0]= d1;
x[n-1]= d2;
for(i=1; i<n-1; i++)
x[i]= x[i-1]+step;
for(i=0; i<n; i++)
x[i]= pow(10,x[i]);
return;
}
/////////////////////////////////////////////////////////////////////////
// generates n logarithmically equally spaced points between
// decades 10^d1 and 10^d2
RVECTOR ms_LogSpace(int d1, int d2, int n)
{
int i;
REAL step;
RVECTOR x(n);
if(n < 2)
ErrorMsg("Error in 'LinSpace': use n >= 2");
step= (REAL)(d2-d1)/(REAL)(n-1);
x(0)= d1;
for(i=1; i<n-1; i++)
x(i)= x(i-1)+step;
x(n-1)= d2;
for(i=0; i<n; i++)
x(i)= pow(10,x(i));
return x;
}