Skip to content

Commit 71a1fe5

Browse files
committed
add example
1 parent f725160 commit 71a1fe5

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

example/linalg/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ ADD_EXAMPLE(state1)
1818
ADD_EXAMPLE(state2)
1919
ADD_EXAMPLE(blas_gemv)
2020
ADD_EXAMPLE(lapack_getrf)
21+
ADD_EXAMPLE(lstsq)

example/linalg/example_lstsq.f90

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
program example_lstsq
2+
use stdlib_linalg_constants, only: dp
3+
use stdlib_linalg, only: lstsq
4+
use stdlib_linalg_state, only: linalg_state_type
5+
implicit none
6+
type(linalg_state_type) :: err
7+
8+
integer, allocatable :: x(:),y(:)
9+
real(dp), allocatable :: A(:,:),b(:),coef(:)
10+
11+
! Data set
12+
x = [1, 2, 2]
13+
y = [5, 13, 25]
14+
15+
! Fit three points using a parabola, least squares method
16+
! A = [1 x x**2]
17+
A = reshape([[1,1,1],x,x**2],[3,3])
18+
b = y
19+
20+
! Get coefficients of y = coef(1) + x*coef(2) + x^2*coef(3)
21+
coef = lstsq(A,b)
22+
23+
print *, 'parabola: ',coef
24+
! parabola: -0.42857142857141695 1.1428571428571503 4.2857142857142811
25+
26+
27+
end program example_lstsq

0 commit comments

Comments
 (0)