使用R我喜欢自动找到最佳觅食图形练习的收益递减曲线的切线。曲线如下所示:
const apiCall = function(searchInput) {
const searchTerm = searchInput
var apigClientFactory = require('aws-api-gateway-client').default;
const config = {
apiKey: 'xxxxxx',
invokeUrl:'https://xxxx.execute-api.us-east-2.amazonaws.com'
}
var apigClient = apigClientFactory.newClient(config);
var params = {
//This is where any header, path, or querystring request params go. The key is the parameter named as defined in the API
// userId: '1234',
search_keyword: 'Ambassador'
};
// Template syntax follows url-template https://www.npmjs.com/package/url-template
var pathTemplate = '/beta/testDB'
var method = 'GET';
var additionalParams = {
//If there are any unmodeled query parameters or headers that need to be sent with the request you can add them here
headers: {
},
queryParams: {
search_keyword: 'Ambassador'
}
}
debugger
apigClient.invokeApi(params, pathTemplate, method, additionalParams)
.then(function(result)
{
//This is where you would put a success callback
return JSON.parse(result.data)
}).catch( function(result){
//This is where you would put an error callback
})
}
切线从点(-40,40)开始,理想情况下,我会在切线所触及的曲线上找到该点。
通过试用&它看起来应该是错误的:
ins <- 40
t <- 30
avg <- 30
curve(ins/2^(x/10+1-1),
0,2*t,xlim=c(-2*t,2*t),ylim=c(ins,0),
xlab="time",ylab="food",type="l",lty=1,col=4,lwd=3,axes=FALSE)
axis(1,pos=ins); axis(2,pos=0)
答案 0 :(得分:1)
设定:
library(sjPlot)
library(sjmisc)
set_theme(base = theme_classic(), axis.title.size = 0, geom.label.size = 4.5,
axis.textsize.x = 1.1, axis.textsize.y = 1.1 )
sjp.frq(base$x, type = c("bar"), sort.frq = c("desc"), geom.colors = "grey")
ins <- 40
t <- 30
avg <- 30
返回一个函数,该函数将函数的值作为主要结果,并将渐变作为属性。
deriv(...,function.arg=TRUE)
我们需要求解等式FUN <- deriv(~ins/2^(x/10+1-1),"x",function.arg=TRUE)
curve(FUN(x),
0,2*t,xlim=c(-2*t,2*t),
ylim=c(ins,0),
xlab="time",ylab="food",type="l",
lty=1,col=4,lwd=3,axes=FALSE)
axis(1,pos=ins); axis(2,pos=0)
(其中((40+x)*D(x)+40=f(x))
是渐变,D(x)
是函数值):
将该等式转换为当等式为真时将返回0的函数:
f(x)
交叉点的衍生物:
rfun <- function(x) {
f <- FUN(x)
(40+x)*attr(f,"gradient")+40-f
}
u1 <- uniroot(rfun,c(-40,60))
绘制细分:
d1 <- attr(FUN(u1$root),"gradient")