74 lines
1.8 KiB
R
74 lines
1.8 KiB
R
# Project 5 for the University of Tulsa's CS-7863 Sci-Stat Course
|
|
# Systems of Equations and Finite Difference Methods
|
|
# Professor: Dr. McKinney, Spring 2023
|
|
# Noah L. Schrick - 1492657
|
|
|
|
## 1. Systems of Equations in Matrix Form
|
|
# a. Use built-in R solve
|
|
A <- matrix(c(2,3,4,3,2,-3,4,4,2),nrow=3)
|
|
b <- matrix(c(3,5,9), nrow=3)
|
|
|
|
x <- solve(A,b)
|
|
x
|
|
|
|
# b. Verify with matrix mult
|
|
all.equal(b, as.matrix(A %*% x))
|
|
|
|
## 2. Matrix from polynomial
|
|
# a. Create vectors for x and y
|
|
xvec <- c(-3,-1.5,0.5,2,5)
|
|
yvec <- c(6.8,15.2,14.5,-21.2,10)
|
|
|
|
# b. Create coefficient matrix
|
|
A.2 <- matrix(ncol=length(xvec), nrow=length(xvec))
|
|
for (i in 1:length(xvec)){
|
|
A.2[,i] = xvec^(i-1)
|
|
}
|
|
A.2
|
|
|
|
# c. Solve
|
|
if (!require("matlib")) install.packages("matlib")
|
|
library(matlib)
|
|
|
|
# verify
|
|
bvec <- solve(A.2, yvec)
|
|
all.equal(as.matrix(yvec), A.2 %*% bvec) # use all.equal instead of == (tols and storage type)
|
|
# EX: identical(as.double(8), as.integer(8)) returns FALSE
|
|
|
|
|
|
|
|
# d. Plot
|
|
poly_predict <- function(x){
|
|
# given input x, returns polynomial prediction
|
|
sum(bvec*c(1,x,x^2,x^3,x^4))
|
|
}
|
|
|
|
xdomain <- seq(min(xvec),max(xvec),.2) # predicted domain
|
|
y.predict <- sapply(xdomain,poly_predict) # predicted range
|
|
plot(xvec,yvec,ylim=c(-50,20)) # plot data
|
|
lines(xdomain,y.predict,type="l") # overlay solid line
|
|
|
|
## 3. Kirchoff
|
|
# a. Create vectors
|
|
# Resistance vec
|
|
# Voltage vec
|
|
# Matrix A and vector b
|
|
# Solve
|
|
# Show currents
|
|
|
|
# b. Repeat a. Use V=200, and R=(5,10,5,15,0,20)
|
|
|
|
## 4. Schrodinger eq
|
|
#a. Solve and plot the 1d quantum harmonic oscillator wave function
|
|
|
|
# b. Solve with Shooting Method
|
|
|
|
## 5. Solve harmonic oscillator Schrodinger with finite difference
|
|
# a. Solve with R and Julia
|
|
|
|
# b. Solve damped oscillator with perturbation param
|
|
# Plot
|
|
|
|
## 6. Solve heat diffusion equation
|
|
|
|
# EC. Solve the square plate problem |