1
votes

I am trying to plot a horizontal line in R, but it is giving me an error.

Code:

w <- seq(1, 99, by=1)
alpha <- 0.1
beta <- 0.001

U <- alpha*w -(beta/2)*w*w
Uprime <- alpha -(beta)*w
Udprime <- -beta
Utprime <- 0

plot(w,Udprime,type = "l",main = "Graph of U(W) versus wealth",xlab = "Wealth",ylab = "Utility Function")

When I plot this function out, I get this error:

Error in xy.coords(x, y, xlabel, ylabel, log) : 
  'x' and 'y' lengths differ

Why is that so? Need some guidance.

1
Because length(w)==99 and length(Udprime)==1? I assume you meant plot(w, Uprime, ...) instead. - Joshua Ulrich
ok thanks, how to solve that problem? - lakesh
Udprime is the second derivative with respect to w, which is constant for all values of w. So a plot would show a horizontal line. What is it that you're trying to actually plot? - Señor O
Maybe you want plot(w, U) ? - Señor O
Or set Udprime <- -beta + 0 * w so that it's length is the same as length(w). - Gregor Thomas

1 Answers

4
votes

To plot the horizontal line for the second derivative of the wealth utility function, you'll need to make sure Udprime has a point for every point w. There's two ways to do this:

Shortcut:

plot(cbind(w, Udprime))

More "true to the math":

Udprime = -beta * w^0