Most adequate method for obtaining data - matlab

Let us suppose I do have two functions on a grid [X,Y]=meshgrid(x,y),
f=f(x,y) and g=g(x,y)
and I want to calculate
sqrt(f^2+g^2)
in two different ways:
Expanding f^2 and g^2 symbolically first and then applying
V1=(f.^2+g.^2).^(1/2).
Using f=f(X,Y), g=g(X,Y) (this is, computing f and g on the grid first) and then applying the puntual operation over the matrices
V2=(f.^2+g.^2).^(1/2)
Unfortunately, when I calculate abs(V1-V2), I don't get zero in all positions.
In my case, the functions have the form
f=f(x,y,cos(x),cos(y),cos(x)cos(y))
and the same for g.
What could be the best method to calculate this operations in order to get the closest result with respect to the analytical function without changing the grid?

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.

Partial Differentiation

I wanted to know command for differentiating a complex Function. I am having trouble formulating the function I want to partially differentiate with respect to Q and then get just optimal Q. fi,Xi, li, Yi ,Qfi and Qsi are the decision Variables. All of this is for ith supplier. One issue I am getting in multiplying 2 variables together for eg. fiSfiXi, how to do that?

How to create a non-linear sequence of numbers given only the starting and ending and number of steps

I am working on tuning a logistic regression model in Apache Spark using cross validation.
I would like to create a range of numbers that follow an exponential curve, e.g. each element in the list is obtained by multiplying the number before it by some constant, C. I will use this range as the regularization options in the paramGrid.
The trick is, I want to do this iteratively, so that the parameters of the best model is used to narrow down the window for the search range. Therefore, after the first iteration, I need a way to programmatically calculate C given X, Y, and N.
If there is a function I can use which will give me what I want, great. Otherwise, what is the formula for calculating C?
You want that
Y = X*C^N
which means that you compute the factor as
C = pow(Y/X, 1.0/N)

MATLAB - Finite element on nonuniform grid

I'm solving a second order differential equation in MATLAB using a finite element method, where I write the second order derivative of a function f as:
d^2f/dx^2 = (f_{i}-f_{i-1}/(x_{i}-x{i-1}) - f_{i+1}-f_{i}/(x_{i+1}-x{i})/(x_{i+1}-x{i-1})/2
Now this operation on f can be translated into a matrix, for which I can then find the eigenvectors, which then are the solutions to the given differential equation.
All this works well for a uniform grid of x-values, i.e. same spacing. But when I try to do it for a nonuniform I get oscillations that should not be there, because the values in the matrix are weighted differently depending on how close the neighbouring grid points are.
Is my approach wrong? Should I use some kind of weighting to take care of the nonuniformity?
I am not sure if I got it right, but the relation you wrote:
d^2f/dx^2 = (f_{i}-f_{i-1}/(x_{i}-x{i-1}) - f_{i+1}-f_{i}/(x_{i+1}-x{i})/(x_{i+1}-x{i-1})/2
looks like finite difference NOT finite element!!!
Otherwise, finite element method does not care (much) about the change of element size from one point to the other in most of the mechanics problems.
If you are handling a finite difference problem, the method does not have to have a regular mesh, but the relations have to be written "carefully" in order to avoid the confusion that may be included in the system matrices.

2-Dimensional Minimization without Derivatives and Ignoring certain Input Parameters on the go

I have a Function V which depends on two variables v1 and v2 and a parameter-Array p containing 15 Parameters.
I want to Minimize my Function V regarding v1 and v2, but there is no closed expression for my Function, so I can't build and use the Derivatives.
The Problem is the following : For caluclating the Value of my Function I need the Eigenvalues of two 4x4 Matrices (which should be symmetric and real by concept, but sometimes the EigenSolver does not get real Eigenvalues). These Eigenvalues I calculate with the Eigen Package. The entries of the Matrices are given by v1,v2 and p.
There are certain Input Sets for which some of these Eigenvalues become negative. These are Input Sets which I want to ignore for my calculation as they will lead to an complex Function value and my Function is only allowed to have real values.
Is there a way to include this? My first attempt was a Nelder-Mead-Simplex Algorithm using the GSL-Library and an way too high Output value for the Function if one of the Eigenvalues becomes negative, but this doesn't work.
Thanks for any suggestions.
For the Nelder-Mead simplex, you could reject new points as vertices for the simplex, unless they have the desired properties.
Your method to artificially increase the function value for forbidden points is also called penalty or barrier function. You might want to re-design your penalty function.
Another optimization method without derivatives is the Simulated Annealing method. Again, you could modify the method to avoid forbidden points.
What do you mean by "doesn't work"? Does it take too long? Are the resulting function values too high?
Depending on the function evaluation cost, it might be an approach to simply scan a 2D interval, evaluate all width x height function values and drill down in the tile with the lowest function values.