0

I would like to plot y against z, where y=(1+z)int_0^z((0.3(1+z')^3+0.7)^-1/2)dz',

where int_0^z just means integrate from 0 to z. I figured I should first integrate from 0 to z, and then use the integral result to plug into the equation. Here is my code:

integrand <- function(z1) {(0.3*(1+z1)^3+0.7)^-1/2}
integral<-integrate(integrand, lower = 0, upper = z)

but this error appears:

Error: object 'any_number' not found

Error in integrate(integrand, lower = 0, upper = z) : object 'z' not found"

How do I define z here?

Thanks,

Jade

Jade
  • 115
  • 7
  • 3
    Something like `z <- any_number` before `integrate`? –  Oct 27 '15 at 00:58
  • 2
    Please edit your post to show the same error after you defined `z`. –  Oct 27 '15 at 01:10
  • 1
    Well, if you had actually given 'z' a value then you might have achieved something. – IRTFM Oct 27 '15 at 01:47
  • 1
    OP shld prbly [re]read https://stat.ethz.ch/R-manual/R-devel/library/base/html/Syntax.html too (I fear the outcome won't be what's expected it to be with current parentical placement. – hrbrmstr Oct 27 '15 at 01:52
  • @BondedDust no this integration is part of the function I'm going to use later so z doesn't refer to a particular value – Jade Oct 27 '15 at 02:00
  • 2
    OK, I see. I give up. –  Oct 27 '15 at 02:50
  • @Jade: you made my day, thanks for the laugh. This is so very similar to the Windows "Press any key" thing, and the response "where is the 'any' key?" Try replacing your literal "any_number" with ... a number. It works. The fact that you claim it "doesn't refer to a particular value" indicates you have many other issues here, starting with your understanding of writing and interacting with functions. (Impressive, though: you've received responses from three prominent SO "R" contributors/gurus. If any of these three suggest something, I recommend taking it as near-gospel.) – r2evans Oct 27 '15 at 04:01
  • @r2evans. First of all I feel very honored to be replied by many of the top writers.... Second the function I'm going to plot is actually y= (1+z)integral_0^z((0.3*(1+x)^3+0.7)^-1/2), so I don't have any values to give to z. I'm really confused here. – Jade Oct 27 '15 at 04:22
  • First, you are writing a function, great. Though there are times when using uninitialized variables in *R* is allowable and perhaps even good practice, I do not believe this is one of them. Perhaps if you provide some context in your question (not in a comment, please) by showing a sufficient portion of the function that (a) ensures `z` is a valid variable, and (b) uses `integral(...)` with these `x`, `y`, and `z` variables. This goes back to [making a good reproducible question](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – r2evans Oct 27 '15 at 04:28
  • Nope, still not clear. Do you already know how you will plot it? Is there a particular plotting function you will be using? Or are using base plotting functions like `points`, `lines`, or even `polygon`, and haven't figured out how to incorporate this nebulous "z" into the fold? Is the plan to show a cumulative density function? – r2evans Oct 27 '15 at 05:08
  • @r2evans oh sorry I made a typo. It should be all about y and z no x. so for integral part I'll get a function which has z, and after multiplying by (1+z) I get relationship between y and z. I'm using curve function. – Jade Oct 27 '15 at 05:15
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/93443/discussion-between-jade-and-r2evans). – Jade Oct 27 '15 at 05:24

1 Answers1

2

I'll give it a try.

integrand <- function(z1) {(0.3*(1+z1)^3+0.7)^-1/2}

We need to make integral a function of z, ensure that it only returns the value of the integral, and that it is vectorized in z:

integral <- Vectorize(function(z) integrate(integrand, lower = 0, upper = z)$value)

Now we can test it:

integral(1:2)
#[1] 0.3056435 0.4037815

And plot:

curve(integral, 0, 10, xname = "z")

resulting plot

Roland
  • 127,288
  • 10
  • 191
  • 288
  • so if I want to plot y=integral*(1+z), I just use integral <- Vectorize(function(z) (1+z)*(integrate(integrand, lower = 0, upper = z)$value)) right? That gives me a straight line. I'm not sure if it's right. – Jade Oct 27 '15 at 14:44
  • @Jade It's not a straight line. Change the axis limits, e.g., `curve(integral, 0, 1e10, xname = "z")`. (And of course, you can always check by solving the integral.) – Roland Oct 27 '15 at 15:13