4

I want to plot Haar functions, which are defined as:

h_1_0 = function(t){
    if ((t >= 0) & (t <= 1/2)){return(1)}
    else if ((t >= 1/2) & (t <= 1)){return(-1)}
    else{return(0)}
}

Then k-th Haar function is:

h = function(t,n,k){return(2^((n-1)/2) * h_1_0((2^(n-1)) * t - k))}

For example I want to draw h(t,1,1), it has to be function:

 1, if 1<=t<=3/2
-1, if 3/2 <=t<=2
 0, otherwise

So how can I plot k-th functions with fixed k and n in R?

joran
  • 169,992
  • 32
  • 429
  • 468
alperovich
  • 854
  • 1
  • 9
  • 14

2 Answers2

2

Create a sequence for the domain. Use appropriate bounds, based on k and n:

x <- seq(.5, 2.5, .001)
plot(x, sapply(x, function(x) h(x,1,1)), pch='.', type='l')
Matthew Lundberg
  • 42,009
  • 6
  • 90
  • 112
  • thanks! How can I integrate this "fixed" function? I just want to work with that like as regular function. – alperovich May 05 '12 at 15:11
  • As implied in Ben Bolker's comment on BenBarnes' answer, you should integrate this mathematically, as it really is trivial. – Matthew Lundberg May 05 '12 at 15:20
  • Since "trivial" doesn't always mean "easy": this (finding the analytical formula for the Schauder function by integrating) is definitely something you should be able to do if you're going to be doing numeric work, but if you try (and get local help) but get stuck you might try http://math.stackexchange.com/ ... – Ben Bolker May 05 '12 at 15:37
  • My question sounds like "how to work with multivariable function as one-variable or more-variable function in R?" I want to fix count variables and get real function for integrating, differentiation etc. – alperovich May 05 '12 at 17:51
  • @Intendia, to give your question "how to work with..." the complete attention it deserves, please post it as a new question to SO. – BenBarnes May 05 '12 at 18:53
2

If you can successfully vectorize your function, which it looks like you can, you can use curve to draw it.

Use Vectorize to create a wrapper to your h function that will allow you to pass a vector to the argument t

Vh <- Vectorize(h, "t")

Use curve to draw the vectorized function

curve( Vh(t = x, n = 1, k = 1), from = .5, to = 5)

curve of Vh(t=x,n=1,k=1)

BenBarnes
  • 19,114
  • 6
  • 56
  • 74
  • thanks! but it helps me to draw this function. What if I want ot work with this function, for example I want to integrate this function from 0 to 1. I have a mistake: Vh(t = x, n = 1, k = 1)' is not a function, character or symbol – alperovich May 05 '12 at 15:07
  • 1
    You probably want to ask a separate question, or possibly push it a little bit and edit your question above to add the new component. In any case, you really need to show exactly what you tried to do ... Just guessing, but you probably want something like `integrate(Vh,lower=...,upper=...,n=1,k=2)` (`integrate` takes a **function** as an argument, not an **expression**). And isn't integrating this function sort of trivial ... ? – Ben Bolker May 05 '12 at 15:11
  • Yeah, I had to get from one end of the room to the other, and @BenBolker beat me to it. `integrate(Vh,lower=0,upper=1,n=1,k=1)` – BenBarnes May 05 '12 at 15:14
  • 2
    also: if you vectorize your original `h_1_0` function by using `ifelse` instead of `if`, you probably don't even need `Vectorize` – Ben Bolker May 05 '12 at 15:15
  • integrating of Haar function gives Schauder function which looks like triangle.. so I want to make somethink like that: Schauder_function_n_k = Integral(indicator_function[0,t](u) * h(u,t,k), from 0 to 1) it means I should integrate haar functions for each k and n. – alperovich May 05 '12 at 15:21