2015-05-25 53 views
0

我試圖建立一個函數來計算矢量運算的大小和方向 爲了解決逆π函數被限制在pi/2域內的問題, -pi/2,如果e小於0,我需要向e添加2pi。但是,一旦添加了if else行,我會在「}」中遇到「Error:unexpected'}'。任何人都可以向我解釋爲什麼?錯誤:「}」中出現意外的'}'

vectorMD <- function(x){ 
    c=x[1:2]-x[3:4] 
    mt <- function(x) {round(sqrt(sum(x^2)),1)} 
    d <- round(mt(c),1) 
    e <- round(atan(c[2]/c[1]),1) 
if(e < 0){e <- e+2*2pi 
    return(e)} 
paste("magnitude =",d,"direction =",e) 
} 

回答

1

而是在二皮如果條件你必須給2 * PI PI是已經像3.14的數值,你不能將它指定爲二皮這就好比是23.14,所以你必須指定它有2 * PI這就好比2 * 3.14

的代碼:

vectorMD <- function(x){ 
    c=x[1:2]-x[3:4] 
    mt <- function(x) {round(sqrt(sum(x^2)),1)} 
    d <- round(mt(c),1) 
    e <- round(atan(c[2]/c[1]),1) 
    if(e < 0){e <- e+2*2*pi 
     return(e)} 
    paste("magnitude =",d,"direction =",e) 
    } 
0

嘗試此,放功能的功能 「MT」 外 「載體」。

mt <- function(x) 
{ 
    round(sqrt(sum(x^2)),1) 
} 

vectorMD <- function(x) 
{ 
    c=x[1:2]-x[3:4] 

    d <- round(mt(c),1) 
    e <- round(atan(c[2]/c[1]),1) 

    if(e < 0) 
    { 
     e <- e+2*2pi 
     return(e) 
    } 

    paste("magnitude =",d,"direction =",e) 
} 
+1

這不是問題@nico你必須指定2pi爲2 * pi – The6thSense