Racket: is it possible to set surface3d steps differently for two axes? - racket

I am trying to create a 3D plot with racket's plot library.
Ho do I make surface3d evaluate my function only in certain points? I mean, I have an function defined only on integers, but surface3d tries to evaluate it somewhere in between them, seemingly according to samples parameter. Is it possible to set that parameter differently for two axes, so that the function would be evaluated only where it is defined, or should I better use some other function for plotting?

If the problem is that surface3d evaluates the function in non-integer points, then
you can define a function that uses the values of the nearby integer points to
calculate an value for points inside the square.
If your functions is called f, then an first attempt is:
(define (g x y)
(f (floor x) (floor y)))
That will give you "stairs".
If you want a smoother appoach you can look at bilinear interpolation.
Bilinear Interpolation - Wikipedia

Related

How to detect and remove outliers in MATLAB?

I have a problem detecting outliers in a set of data. Let's say I have two arrays x and y, and y is a quadratic function of x. Some of the values of y do not follow this function. How can I detect them?
I tried the rmoutliers function, but it doesn't seem to solve this problem since it only deals with normally distributed data.
Basically, I am trying to study some material behavior. The behavior is represented by y. I use an optimization method to get the different values of y as a function of x. Because sometimes the optimization doesn't yield accurate results, I get outliers.The relationship I am expecting should follow some nearly quadratic function, but the coefficients of this function are variable based one the provided set of data, so I can't use a certain function of x and use it to detect the outliers in the array of y values.

Interpolate over specified axis in MATLAB

I have an n-dimensional matrix, funtointerpolate, and I wish to perform one dimensional interpolation along one of its axes (let's call it axis m). In Python, interpolate functions such as interp1d allow one to specify the axis of interpolation. In MATLAB, I cannot see an obvious way to do this using interp1 or any other built-in interpolate functions. Ideally, the function would look something like
interpolatedfun = interp1(funtointerpolate,oldpoints,newpoints,axis = m)
An obvious way to get around this is to loop over all the other axes in funtointerpolate, but this is rather cumbersome. The motivation for interpolation is that the data in funtointerpolate is evaluated along a non-uniform grid along the m axis. I need it to be uniform along m. Mathematically, suppose I have some tensorial object
A_{ijk}
which is evaluated along a non-uniform grid along the j index. Then, I wish to find a new A such that the jth index consists of values evaluated on a uniform grid. I know the new uniform grid for the jth index, newpoints, and the old grid oldpoints.
You can use the interpn function for this purpose:
newV = interpn(oldAx1, ..., oldAxM, ..., oldAxN, oldV, ...
oldAx1, ..., newAxM, ..., oldAxN);
where V is your output.
(Of course the above is pseudo-code, but it should nicely illustrate the way to solve your problem.)

Matlab fit with function returning vector with all the y(x_i) values

I am working on something and I haven't found any solution, maybe I didn't know how to correctly search for it...
I have two arrays of experimental data (x and y). x is a list of certain energies (512 values from 0 to 100 kev) and I want to fit them to a function which returns a vector of values of y for every x in the list (the energies are always the same, 512 certain values). This is because my function model contains several matrix and other functions.
So, I can't evaluate my function as f(x,a,b,c...) (with a,b,c the parameters to fit) and expect a single scalar, but I have to evaluate f(a,b,c...), and it returns a vector of y(x1),y(x2)...
Now, I want to fit my data to my model. But lsqcurvefit needs a function of the form f(x), I suppose that it evaluates every f(x). I could write my function so that every time it is called it evaluates the vector result, and then returns y for the given x, but it would be quite inefficient... And I'm sure there must be another way.
Any idea?
Maybe you can do an fminsearch on the sum of square errors? It is best to put all fitting parameters into one vector. Here I call it p.
f = #(x,p) (p(3)+p(1)*x.^p(2)).^(1/p(4); %example function with four free parameters
sqerr = #(x,y,p) sum((y-f(x,p)).^2); %sum of squared errors
p = [1,1,1,1]; %four starting conditions
p = fminsearch(#(p) sqerr(x,y,p),p); %fit
Then you can find your y(x_i) values by calling the function with the fitted paramters
f(x,p)

MATLAB differentiation when the derivative of a function is numerically known

I want to differentiate the following function wrt in MATLAB:
T(e(x(t),t)⁄p(t))
My problem is that I know the derivatives of x numerically (I am inside a kind of odefun).
I want to use diff to make my code generalizeable for high order derivatives,but the derivatives of x are now constant. I would also like all this to be in an anonymous function where I can make the differentiation and substitute accordingly the time and the derivative of x needed,so that I don't have to write multiple functions for every state of my system.
My code is as follows:
syms q x star;
qd=symfun(90*pi/180+30*pi/180*cos(q),[q]);
p=symfun(79*pi/180*exp(-1.25*q)+pi/180,[q]);
T=log(-(1+star)/star);
e=symfun(x-qd,[x,q]);
and I want to write for example a function in the form
#(t,y)(d^2⁄dt^2 T(e(x(t),t)⁄p(t))+d⁄dt T(e(x(t),t)⁄p(t))+T(e(x(t),t)⁄p(t)))
I am not sure of the implementation details but in general this is one approach that you could take. It involves two steps.
in the T(.) function replace x with exp(t) this way when you do the differentiation exp(t) always stays there for the higher order derivatives to be taken and the outer functions will be differentiated with respect to x at the same time. After you do diff you should receive an expression that contains exp(t) (not tested so hopefully it is the case). At this point exp(t) is your time derivative of x. Now you only need to evaluate this expression in t. When doing so you need to replace exp(t) by the derivative of x. I do not know if this can be done, if not then perhaps using y instead of exp(t) with the constraint y=exp(t) would do it but you need to figure the correct implementation out yourself.
Here you need to substitute the derivative of x at the right t. If you do not have the value at the particular point t then do what I suggested in the comment. Pre-calculate it beforehand in many points and interpolate in this step.
This approach relies on swapping x(t) with exp(t) if that does not work then I would do what I suggested in the comment. Approximate x(t) by a known function and use that instead of x in your code.

Can MATLAB plot a constrained symbolic function?

I'm trying to plot the following:
syms f_ih n
bi = (1-f_ih)/2
where f_ih is constrained by: 0 ≤ f_ih ≤ (n-1)/n. I'd like to show bi as a surface plot with independent variables n and f_ih, but ezsurf doesn't allow for variable bounds. How can I do this?
The command I'd love to run would simply be ezsurf(bi,[0,(n-1)/n]), but it's not that simple.
Thanks!
There's a good reason for requiring the domain to be numeric as opposed to symbolic: the function has to be evaluated in order to obtain actual numbers that can be plotted. It makes no sense to plot something that is purely symbolic like your equations (in Matlab or on paper) unless you specify n. Some functions may be scale invariant (not quite the right term mathematically), meaning they look the same when evaluated/plotted on different domains, but Matlab has no way of knowing. Choose a value of n that results in a plot that looks how want. Then, if you like, you can remove the numbers from the axes afterwards and label them as if it were for arbitrary n even though it's not.