I have a homework assignment where I have to write a Bessel function and compare it to the built in Bessel function in r. I have to plot the curve on the interval (0.01:10). The function has several parts and for the interval 0<=x<=3 I have to find x using equation 1. On the interval 3 < x <= infinity I have to use equation 2. I've tried the if and else but I kept having the "length >1 so first element is used" message. I found out that for vectors I need the ifelse statement but how do I actually use it? I played with it and found that it only does true/false type stuff. i.e. ifelse(x<=3, y, z) where all the numbers below and equal to x are y and all numbers greater than x are z. How would I go about writing a function where it does equation 1 if x <=3 and equation 2 for all other numbers?
The code I provided is the wrong one and it may be sloppy but I've been playing around with r for one week now and this is about as go
x <- seq(.01,10, .01) #sequence of numbers 0.01 - 10 in 0.01 intervals.
#Bessel function for a set of numbers
bess.J = function(x){
if(x<=3){
#
less3 = 1-2.249997*(x/3)^2+1.2656208*(x/3)^4-0.31638*(x/3)^6+0.044479* (x/3)^8-0.0039444*(x/3)^10+0.00021*(x/3)^12
return(less3)
}
#
else{
Tgreater3 = x - 0.78539816 - 0.04166397*(3/x) - (0.00003954*(3/x)^2) + (0.00262573*(3/x)^3) - (0.00054125*(x/3)^4) - (0.00029333*(3/x)^5) + (0.00013558*(3/x)^6)
Fgreater3 = 0.79788456 - 0.0000077*(3/x) - (0.00552740*(3/x)^2) - (0.00009512*(3/x)^3) + (0.00137237*(3/x)^4) - (0.00072805*(3/x)^5) + (0.00014476*(3/x)^6)
Jgreater3 = x^(-1/2)*Fgreater3*cos(Tgreater3)
return(Jgreater3)
}
}
plot(x,bess.J(x))