楼主: Dylan890328
10157 8

30金币悬赏,用R画简单函数图像 [推广有奖]

  • 0关注
  • 0粉丝

硕士生

36%

还不是VIP/贵宾

-

威望
0
论坛币
2 个
通用积分
0
学术水平
3 点
热心指数
0 点
信用等级
0 点
经验
931 点
帖子
76
精华
0
在线时间
184 小时
注册时间
2010-9-15
最后登录
2022-6-22

楼主
Dylan890328 发表于 2011-1-13 19:02:19 |AI写论文
30论坛币
急求R 指令, 画下面三个概率密度函数的图像:1. f(x)=4/3 * (1-x^3)     x属于区间(0,1)
2. f(x)=3/7 * x^2         x 属于区间 [1,2]
3. f(x)=1/10       x属于区间 [-2,8]

  1. > a<-0
  2. > b<-1
  3. > myfunc<-function(x,a,b){
  4. + out<-rep(0,length(x))
  5. + out[x>a & x<b]<-(4/3)*(1-x[x>a & x<b]^3)
  6. + return(out)
  7. + }
  8. > plot(x, myfunc(x,a,b), type = "l", col="blue", lwd=4)[code]
复制代码
错误于plot(x, myfunc(x, a, b), type = "l", col = "blue", lwd = 4) :
  找不到对象'x'
这是老师给的第一题的code,但运行出来有错误,哪位高人指点一下


最佳答案

楚韵荆风 查看完整内容

a=0;b=1 x=seq(a,b,by=0.01) fun1=function(x){ 4/3*(1-x^3)} plot(x, fun1(x), type = "l", col="blue", lwd=4) a=1;b=2 x=seq(a,b,by=0.01) fun2=function(x){ 3/7*x^2} plot(x, fun2(x), type = "l", col="red", lwd=4) a=-2;b=8 x=seq(a,b,by=0.01) fun3=function(x){ n=length(x);re=rep(0,n);for(i in 1:n){ re=0.1} } plot(x, fun3(x), type = "l", col="green", lwd=4)
关键词:概率密度函数 概率密度 密度函数 Fun UNC 函数 金币 悬赏 图像

回帖推荐

楚韵荆风 发表于7楼  查看完整内容

或者用: fun1=function(x){ 4/3*(1-x^3)} fun2=function(x){ 3/7*x^2} fun3=function(x){ n=length(x);re=rep(0,n);for(i in 1:n){ re=0.1} } final.fun=function(fun=c("fun1","fun2","fun3"),a,b,eps=0.01) { x=seq(a,b,by=eps) plot(x,fun(x), type = "l", col="red", lwd=4) } final.fun(fun1,0,1,eps=0.01)

楚韵荆风 发表于2楼  查看完整内容

a=0;b=1 x=seq(a,b,by=0.01) fun1=function(x){ 4/3*(1-x^3)} plot(x, fun1(x), type = "l", col="blue", lwd=4) a=1;b=2 x=seq(a,b,by=0.01) fun2=function(x){ 3/7*x^2} plot(x, fun2(x), type = "l", col="red", lwd=4) a=-2;b=8 x=seq(a,b,by=0.01) fun3=function(x){ n=length(x);re=rep(0,n);for(i in 1:n){ re=0.1} } plot(x, fun3(x), type = "l", col="green", lwd=4)

本帖被以下文库推荐

沙发
楚韵荆风 学生认证  发表于 2011-1-13 19:02:20
a=0;b=1
x=seq(a,b,by=0.01)
fun1=function(x){ 4/3*(1-x^3)}
plot(x, fun1(x), type = "l", col="blue", lwd=4)
a=1;b=2
x=seq(a,b,by=0.01)
fun2=function(x){ 3/7*x^2}
plot(x, fun2(x), type = "l", col="red", lwd=4)
a=-2;b=8
x=seq(a,b,by=0.01)
fun3=function(x){ n=length(x);re=rep(0,n);for(i in 1:n){ re[i]=0.1} }
plot(x, fun3(x), type = "l", col="green", lwd=4)
共享是一种彼此的快乐

藤椅
fairy9939 发表于 2011-1-13 19:54:28
冒昧的回个帖呀!
第三个是不是酱紫,
x=seq(-2,8,length=200)
y=dunif(x,min=-2,max=8)
plot(x,y,type="l",xlim=c(-4,10),ylim=c(0,0.2),lwd=2)

板凳
Dylan890328 发表于 2011-1-13 20:26:50
  1. > a<-0
  2. > b<-1
  3. > myfunc<-function(x,a,b){
  4. + out<-rep(0,length(x))
  5. + out[x>a & x<b]<-(4/3)*(1-x[x>a & x<b]^3)
  6. + return(out)
  7. + }
  8. > plot(x, myfunc(x,a,b), type = "l", col="blue", lwd=4)[code]
复制代码
错误于plot(x, myfunc(x, a, b), type = "l", col = "blue", lwd = 4) :
  找不到对象'x'
这是老师给的第一题的code,但运行出来有错误,哪位高人指点一下

报纸
楚韵荆风 学生认证  发表于 2011-1-13 20:36:26
你这个程序里面没有x变量,当然会出这样的错误
共享是一种彼此的快乐

地板
Dylan890328 发表于 2011-1-13 20:49:15
4# 楚韵荆风 那该如何定义这个X变量

7
楚韵荆风 学生认证  发表于 2011-1-13 20:55:54
或者用:
fun1=function(x){ 4/3*(1-x^3)}
fun2=function(x){ 3/7*x^2}
fun3=function(x){ n=length(x);re=rep(0,n);for(i in 1:n){ re[i]=0.1} }
final.fun=function(fun=c("fun1","fun2","fun3"),a,b,eps=0.01)
{ x=seq(a,b,by=eps)
  plot(x,fun(x), type = "l", col="red", lwd=4)
}
final.fun(fun1,0,1,eps=0.01)
共享是一种彼此的快乐

8
楚韵荆风 学生认证  发表于 2011-1-13 20:56:11
见楼上~~~~~~~~
共享是一种彼此的快乐

9
suzhzh 发表于 2011-1-13 21:09:44
真强,上面说得都是太好了,不错支持

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-9 07:00