Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hey I just need help fixing some syntax in R studio, it is as follows: Error message: Error in xy.coords(x, y) : 'x' and 'y'

Hey I just need help fixing some syntax in R studio, it is as follows:

Error message: "Error in xy.coords(x, y) : 'x' and 'y' lengths differ"

All of my code compiles perfectly, but right at my last call for plotting my x and y vectors I get the error shown above..... What am I doing wrong?

#Q2 part 2 CUBIC SPLINE

CubicSpline = function(x,y,dy0,dyn) { n = length(x) m = length(x) A = matrix(nrow = n, ncol = m) for ( i in 2:(n-1)) { for ( j in 1:n) { if (j==i || j==i+1 || j==i-1) {A[i, j] = 2} else {A[i, j] = 0} } } A[1,1] = 1 for ( i in n) { A[i,i] = 1 } for ( i in 1) { A[i,i] = 1 } b = c() b[1] = dy0 b[n] = dyn for (i in 2:(n-1)) { b[i] = 3*((y[i]-y[i-1])/(x[i]-x[i-1]) + (y[i+1]-y[i])/(x[i+1]-x[i])) } return(solve(A,b)) }

# Cubic Spline value

CubicSplineValue = function(x_value,x,y,dy) { dy=c() y=c() #find the interval x_value lies in i = 1 while (x_value >= x[i]) { if (x_value == x[i]) { return(y[i]) } else { if (x_value < x[i+1]) { #x is between x[i] and x[i+1] break } i = i + 1 } } return(x_value) #now we must find ai and bi for the given interval x_value is in #can we find it using our algorithm? aii =c() bii = c() aii[i] = ((x[i]-x[i-1])*dy[i-1] -(y[i]-y[i-1])) bii[i] = (-(x[i]-x[i-1])*dy[i] +(y[i]-y[i-1])) t = (x_value - x[i]) / (x[i+1] - x[i]) result = (1-t)*y[i] + t*y[i+1] + t*(1-t)*(aii*(1-t) + bii*t) result } dy = CubicSpline(0:10, sin(0:10), cos(0), cos(10)) x = c() y = c() for (i in 0:100) { x = c(x, i*0.1) y = c(y, CubicSplineValue(i*0.1, 0:10, sin(0:10), dy)) } plot(x,y) #this is where the error occurs

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Processing

Authors: David M. Kroenke

12th Edition International Edition

1292023422, 978-1292023427

More Books

Students also viewed these Databases questions

Question

How wide are Salary Structure Ranges?

Answered: 1 week ago