Overview

SC19017016 is a simple R package developed by SC19017016 to solve some basic problems when you are learn the Mathematical modeling course.Two functions are considered namely, npboy (to solve the newspaper boy problem) and steel (to deal with the problem about wasting in steel rolling). In the next section, we will introduce the background about the two question and how to use the two functions.

npboy

There is a common case that the number of people who want to buy a newspaper is not a fixed value. We have reason to assume it subjects to the normal distribution. We continue to assume that the newspaper retail price, purchase preice and rebate is a, b and c(clearly, a>b>c).As a newspaper boy, it's important to determine the number of newspaper because if the number of buyers is greater than the number of purchases newspaper boy may make a lot less money. On the country, great loss will come if he have excessive optimism about the market.

The source R code for npboy is as follows:

npboy<-function(a,b,c,miu,sigma){
  n_day<-numeric(365)
  revenue<-function(x,guest){
    if(guest<=x)
      r<-(a-b)*guest-(b-c)*(x-guest)
    else
      r<-(a-b)*x
    return(r)
  }
  for (i in 1:365) {
    guest<-rnorm(1,miu,sigma)
    temp<-numeric(6*sigma+1)
    paper<-seq(from=miu-3*sigma,to=miu+3*sigma,by=1)
    for(j in miu-3*sigma:miu+3*sigma){
      temp[j]<-revenue(j,guest)
    }
    n_day<-paper[which.max(temp)]
  }
  return(mean(n_day))
}

steel

Steel needs to go through rough rolling and finishing rolling, if the length of rough rolling steel is less than the target, then the whole steel scrap, if the length of rough rolling steel is greater than the target, then the target length got through finishing rolling. The variance of the rolling mill is determined by the machine itself, but the mean value is set artificially. We want to find an optimal value to minimize the waste of steel.

The source R code for steel is as follows:

steel<-function(L,sigma,pre){
  n<-10000
  loss<-function(l){
    temp<-numeric(length(l))
    for (i in 1:length(l)) {
      if(l[i]>L)
        temp[i]<-l[i]-L
      else
        temp[i]<-l[i]
    }
    return(temp)
  }
  tr<-seq(from=L,to=L+3*sigma,by=pre)
  true_loss<-numeric(length(tr))
  for (i in 1:length(tr)) {
    l<-rnorm(n,tr[i],sigma)
    temp<-loss(l)
    true_loss[i]<-sum(temp)/sum(l>L)
  }
  return(tr[which.min(true_loss)])
}

Exapmle

some possible outcomes may be generated by the two functions.

a<-3
b<-2
c<-0.8
miu<-500
sigma<-60
optimal_number<-npboy(a,b,c,miu,sigma)
L<-120
sigma<-5
pre<-0.5
optimal_length<-steel(L,sigma,pre)


SC19017016/SC19017016 documentation built on Dec. 31, 2019, 12:53 a.m.