Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. library(plotly)
  2. library(dplyr)
  3.  
  4. Non_Lin_Regressie <- function(x,y){
  5. # Solving the least squares method
  6. Pol_Matrix <- matrix(c(x^2,x,rep(1,length(x))),ncol=3)
  7. LHS_pol <- t(Pol_Matrix) %*% Pol_Matrix # Computes the LHS
  8. RHS_pol <- t(Pol_Matrix) %*% y # Computes the RHS
  9. x_sol_pol <- solve(LHS_pol,RHS_pol) # Solves the equation
  10. return(x_sol_pol)
  11. }
  12.  
  13. x <- c(-1,-1/2,0,1/2,1)
  14. y<- c(2,1,0,1,2)
  15. sol <- Non_Lin_Regressie(x,y)
  16. func <- sol[1]*x^2 + sol[2]*x+sol[3]
  17. plot_ly(x=~x, mode= 'lines') %>%
  18. add_trace(y = ~func, line = list(shape = "spline"), name= 'solved by LS') %>%
  19. add_trace(y=~y, name= 'normal plot')
  20.  
  21. [,1]
  22. [1,] 1.7142857
  23. [2,] 0.0000000
  24. [3,] 0.3428571
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement