Mathematica not evaluating cell. (Fourier Series) - wolfram-mathematica

I am trying to get Mathematica to evaluate the following function I entered in a cell, but it did not perform anything. And then I want to plot G[t].
G[t_] = 1/2 + Sum[2/(k * Pi) * Sin[k*Pi/2], (K, 0, 19)];
Is there something wrong with how I entered it in the cell? I am to getting any errors related to brackets.

Related

How does Func.realize in Halide works?

I can understand the explanation in tutorial 6, which is:
// Func gradient("gradient");
// Var x("x"), y("y");
// gradient(x, y) = x + y;
// gradient.realize(8, 8);
//
// This does three things internally:
// 1) Generates code than can evaluate gradient over an arbitrary
// rectangle.
// 2) Allocates a new 8 x 8 image.
// 3) Runs the generated code to evaluate gradient for all x, y
// from (0, 0) to (7, 7) and puts the result into the image.
// 4) Returns the new image as the result of the realize call.
However, follow the description, I can't figure it out how such a example works:
Func histogram("hist_serial");
histogram(i) = 0;
RDom r(0, input.width(), 0, input.height());
histogram(input(r.x, r.y) / 32) += 1;
histogram.vectorize(i, 8);
histogram.realize(8);
What I am confusing is: in the "gradient" example, evaluating gradient for all x, y from (0,0) to (7,7) can give us a result, like gradient(1,1)=1+1=2. But in the second example, evaluating histogram for i from 0 to 7 looks strange to me, as I think that we are trying to calculate the result from back to front. A more natural way is to evaluate the input first, then calculate histogram.
So, how the "realize" in the second example works?
Halide automatically infers all of the values which need to be computed to produce a requested region of output. realize just asks the pipeline to compute the requested region of the output Func(s). Halide then automatically infers what regions of which earlier Funcs are required, and recursively evaluates all of the those, up to the inputs, before producing the requested region of output.

Fitting data to unknown curve -- Possible tanh

I am trying to fit the following dataset:
0.01 3.69470157
0.59744 3.514991345
0.65171 3.265043489
0.70076 2.978933734
0.75021 2.700637918
0.80103 2.413791532
0.84878 2.086939551
0.89572 1.819489189
0.94717 1.532756131
0.99626 1.244667864
1.01643 1.130430784
1.03626 1.024324017
1.05633 0.910153046
1.07605 0.804981232
1.09791 0.708171108
1.11795 0.612456485
1.13841 0.516217721
1.15944 0.421844141
1.18032 0.335218393
1.20003 0.258073446
1.22204 0.181296813
1.24223 0.115157866
1.25935 0.069310744
Where the first column is x and the second is y.
I have tried a tanh function, polynomials, and now trying the erf function. Nothing seems to fit correctly.
Is there a way to know what function I should be fitting to this? And if so, what is the form of such a function. Thank you.
BIG EDIT: the function must be monotonically decreases as x increases, and have asymptotic behavior at the tail ends. So for the data-set it looks like it should approach ~3.7 and ~0.0
A simple sine(radians) equation with an offset gives a good fit:
y = amplitude * sin(pi * (x - center) / width) + Offset
amplitude = -2.2364202059901910E+00
center = 8.6047683705837374E-01
width = 1.1558269132014631E+00
Offset = 2.0456549443735259E+00
R-squared: 0.99994
RMSE: 0.00909

Plotting two variables function

This question is for learning purpose. I am writing my own function to plot an equation. For example:
function e(x) { return sin(x); }
plot(e);
I wrote a plot function that takes function as parameter. The plotting code is simple, x run from some value to some value and increase by small step. This is plot that the plot() manage to produce.
But there is the problem. It cannot express the circle equation like x2 + y2 = 1. So the question would be how should the plot and equation function look like to be able to handle two variables.
Noted that I am not only interesting in two circle equation. A more generalize way of plotting function with two variables.
Well to plot a non function 1D equation (x,y variables) you have 3 choices:
convert to parametric form
so for example x^2 + y^2 = 1 will become:
x = cos(t);
y = sin(t);
t = <0,2*PI>
So plot each function as 1D function plot while t is used as parameter. But for this you need to exploit mathematic identities and substitute ... That is not easily done programaticaly.
convert to 1D functions
non function means you got more than 1 y values for some x values. If you separate your equation into intervals and divide to all cases covering whole plot then you can plot each derived function instead.
So you derive y algebraicaly (let assume unit circle again):
x^2 + y^2 = 1
y^2 = 1 - x^2
y = +/- sqrt (1 - x^2)
----------------------
y1 = +sqrt (1 - x^2)
y2 = -sqrt (1 - x^2)
x = <-1,+1>
this is also not easily done programaticaly but it is a magnitude easier than #1.
do a 2D plot using equation as predicator
simply loop your view through all pixels and render only those for which the equation is true. So again unit circle:
for (x=-1.0;x<=+1.0;x+=0.001)
for (y=-1.0;y<=+1.0;y+=0.001)
if (fabs((x*x)+(y*y)-1.0)<=1e-6)
plot_pixel(x,y,some_color); // x,y should be rescaled and offset to the actual plot view
So you just convert your equation to implicit form:
x^2 + y^2 = 1
-----------------
x^2 + y^2 - 1 = 0
and compare to zero with some threshold (to avoid FPU accuracy problems):
| x^2 + y^2 - 1 | <= threshold_near_zero
The threshold is half size of plot lines width. So this way you can easily change plot width to any pixel size... As you can see this is easily done programaticaly but the plot is slower as you need to loop through all the pixels of the plot view. The step for x,y for loops should match pixel size of the view scale.
Also while using equation as predicate you should handle math singularities as with blind probing you will most likely hit some like division by zero, domain errors for asin,acos,sqrt,etc.
So for arbitrary 1D non function use #3. unless you got some mighty symbolic math engine for #1 or #2.
Defination of a function : A function f takes an input x, and returns a single output f(x).
Now it means for any input there will be one and only one unique output. Like y = sin(x). this is a function on x and y definnes that function.
For equaltion like (x*x) + (y*y) = 1. there are two possible values of y for a single value of `x, hence it can not be termed as a valid equaltion for a function.
If you need to draw it then one possible solution is to plot two points for a single value of x, i.e. sqrt(1-(x*x)) and other -1*sqrt(1-(x*x)). Plot both the values (one will be positive other will be negative with the same absolute value).

MATLAB: how to transform one image to another image

I am trying to transform one image to another in MATLAB, and in the meantime also get the transformation function T(x,y) from this operation (eg. T(x,y) = (x + a(x,y), y + b(x,y)) ). Let's use the following figures as an example. I want to transform the square in the first figure to the circle in the other figure, and output gives me a transformation function, so whenever I use the function on the square I will get the circle.
Functions I have looked into:
I have looked into imwarp, but it should be only for geometric transformation (scaling, rotating, shearing). I don't think this is useful in transforming a square to a circle, while not providing any transformation matrix beforehand.
I have looked into using imregconfig and imregister from a MATLAB example, but it seems to only work for images with same structure but different intensities. Plus, it doesn't give out any transformation function. Please correct me if I am wrong.
Thank you in advance for any help!
The solution space for valid transformations is infinite. How do we choose the "right" one?
One possible transformation for those inputs is to translate the square -0.5 in x and -0.5 in y. Then scale it to double the size (x' = 2x, y' = 2y) and apply a square-to-circle mapping function. (Solution 1)
Another completely valid transformation function could run all the points in the input through the expression
( (x == 0 || x == 1) && (0 <= y <= 1) || ( (y == 0 || y == 1) && (0 <= x <= 1) )
and if all points pass, simply print out "x^2 + y^2 = 1". (Solution 2)
Solutions (1) and (2) both produce code that seems to satisfy the case you've presented, but clearly (2) is so non-general as to be useless. I think you need to define the problem more precisely before we can start writing a solution in code.

Chordal Catmull-Rom Splines

I've been working on getting Catmull-Rom splines working for a side project and am having difficulty getting it to do what I need. I tried the following two implementations and both didn't work for me, and I was unable to track down any errors in my code relative to theirs (which I have to assume has been tested). I'll call theirs the "ABC" solution:
Catmull-rom curve with no cusps and no self-intersections
https://en.wikipedia.org/wiki/Centripetal_Catmull%E2%80%93Rom_spline
I then implemented the following solution (that I call the "Matrix" solution) and it did work using the edited version 3 posts down: https://www.opengl.org/discussion_boards/showthread.php/159518-catmull-rom-spline
However, this Matrix solution just implements Catmull-Rom with a 0.5 'a' value built into the matrix. I'd like to get Chordal working, and thus I need 'a' == 1.
Given that my solution for the ABC version was causing problems, I've attempted to use the matrix here (http://algorithmist.net/docs/catmullrom.pdf) to pass in my own 'a'. Here's the original 0.5 code followed by my modified code that's passing in a user specified 'a'.
Original Code:
float u2 = u * u;
float u3 = u2 * u;
return ((2 * x1) +
(-x0 + x2) * u +
(2*x0 - 5*x1 + 4*x2 - x3) * u2 +
(-x0 + 3*x1 - 3*x2 + x3) * u3) * 0.5f;
Modified Code:
float u2 = u * u;
float u3 = u2 * u;
static float a = 0.5f;
return ((1.0f * x1) +
((-a*x0) + (a*x2)) * u +
((2.0f*a)*x0 + (a-3.0f)*x1 + (3.0f-(2.0f*a))*x2 + (-a*x3)) * u2 +
((-a*x0) + (2.0f-a)*x1 + (a-2.0f)*x2 + (a*3.0f)) * u3) * 0.5f;
This of course doesn't work. However, I'm not seeing why. At the bottom of page 4 in the pdf it shows the matrix with 'a' in it. I've substituted that in the above modified code and triple checked it, yet the spline is screwed up. It should have given me the same answer. What's doubly confusing is that his results on page 5 take that resulting matrix and multiply it by 0.5 which drops all the /2's off the matrix entries. The final matrix uses THESE values, but the original matrix on page 4 is not 0.5 * matrix, it's just "matrix". Why was this 0.5 arbitrarily added and why does everything break without it?
Anyway, does anyone know what I might be doing wrong with my equation? Can I use this matrix form to pass in my own 'a' from 0-1 and create uniform, centripetal and chordal splines or will I have to use the ABC form?
Thanks in advance!
I think the matrix with 'a' in page 4 of the pdf file is still for uniform Catmull-Rom (CR) spline. The parameter 'a' is the tension parameter. In the Wiki page (https://en.wikipedia.org/wiki/Centripetal_Catmull%E2%80%93Rom_spline), it also use a 'alpha' for controlling the knot sequences assigned to each point. Don't confuse the tension parameter 'a' with this 'alpha'.
A "standard" uniform CR spline will have alpha=0.0 (which will result in a=0.5). You will need to use alpha=1.0 for chordal CR spline and alpha=0.5 for centripetal CR spline. Their corresponding matrix form will both involve the knot sequences of the points. So, using a=1.0 in the matrix form for uniform CR spline will not result in a chordal CR spline but a uniform CR spline with stronger tangents at the data points, which typically will cause undesired spline shape.

Resources