Methods for smoothing contour lines - matlab

I have a database P with columns X, Y and Z:
x=0:1:50;
r=3.*rand(1,51);
P=[cos(x')+r',sin(x')+r',sin(x'+r').*cos(x')+r'];
P = sortrows(P,[1,2]);
N = 500;
xv = linspace(min(P(:,1)), max(P(:,1)), N);
yv = linspace(min(P(:,2)), max(P(:,2)), N);
[X,Y] = ndgrid(xv, yv);
Z = griddata(P(:,1), P(:,2), P(:,3), X, Y);
contourf(X, Y, Z, 35)
With the code above, I get the following subplot (right):
This "angularity" arises due to the addition of a vector r of random values ​​to the data. How to smooth out this angularity and make the graph smoother?
I tried to increase N to 2500. It did not give a significant result (in fact, it stopped changing significantly after N=1500).

To smooth your 2D data you can use a 2D convolution, with the operator conv2
With your example data:
n = 10 ;
kernel = ones(n)/n.^2 ;
Zs = conv2(Z, kernel,'same') ;
contourf(X, Y, Zs, 35) ;
title(sprintf('Filter size: n=%d',n))
will yield:
You have to adjust the filter size (the parameter n) until you get the desired result. For example with n=20 you will get:
And for n=50:
A few things to keep in mind:
Since you have NaN in your matrix Z, the filtering will erode the border of you initial domain. The stronger the filtering/smoothing (higher n), the more erosion you will notice until very little non-NaN data is left .
For it to be a smoothing operation and not another transform or filtering, the convolution kernel has to be built so (i) all the elements are of equal value, and (ii) the total sum of the elements should be 1.
A quick demonstration with n=2:
>> n=2
n =
2
>> kernel = ones(n)/n.^2
kernel =
0.25 0.25
0.25 0.25
>> sum(sum(kernel))
ans =
1

Related

Rotate discrete dataset Octave

I have a discrete dataset, X=[x1,x2,..,x12] & Y=[y1,y2,...,y12]. X ranges from [-25, 0] and Y ranges from [1e-6, 1e0]. X does not increase uniformly - as x approaches a value of 0, data sampling density increases from increments of 2.5 to increments of 1. Each x value is units of cm. I cannot get a good fit to the data from fitting a function (I've tried quite a few). I'm left with the discrete data. My need is to sweep the X, Y data completly around the Z axis and put the resulting swept data values into a matrix Z of size (51, 51). I've tried using the cylinder function, [u,v,w] = cylinder(Y) thinking I could extract the data or create a matrix Z from [u, v, w]. I can't seem to sort that out. surf(u,v,w) plots almost correctly - the scaling on the (u, v) axes ranges from [-1, 1] instead of [-25, 25]. This is, I assume, because I'm using cylinder(Y). When I try [u,v,w] = cylinder(X,Y) I get, error: linspace: N must be a scalar. It seems like there should be a better way then my approach of using cylinder to take the X & Y data, interpolate between points (to fill Z where data isn't), rotate it, and put the result into a matrix Z. Any suggestions are welcome. I'm using Octave 6.3.0. Thank you in advance.
Create a matrix R containing distance from origin values.
Use for loops and single value interpolation to cover the R space.
Place the interpolated values into the matrix Z.
% Original data X = [-25,-22.5,...,0]; size(X) = 1 12
% Original data Y = [1e-6, 1.3e-6,...,1] size(Y) = 1 12
u = [-range(X):1:range(X)]; v = [-range(X):1:range(X)]';
R = -sqrt(u.^2.+v.^2);
Z = zeros( 2 .* range(X) + 1);
for i = 1:size(R,1)
for j = 1:size(R,2)
if R(i,j) < min(X); Z(i,j) = 0; endif
if R(i,j) >= min(X); Z(i,j) = interp1(X,Y,R(i,j)); endif
endfor
endfor

MATLAB: summing out one variable in equation

I have the variables
X = 1x20 vector of doubles
i = 0:M
j = 0:M
And the equation
sum n=1 to length(X) : (X(n)^(i+j))
Is there a way to obtain an MxM matrix (through the indices i,j) while summing out n in each cell? I tried this with symsum but it doesn't allow indexing with n.
Any help is appreciated!
By reshaping X to a vector of size [1 x 1 x 20] and using implicit expansion a 3D [M+1 x M+1 x 20] array is created then by summing along the third dimension the result can be obtained.
X = rand(1,20);
M = 30;
ii = 0:M;
jj = (0:M).';
Y = reshape(X,1,1,[]);
result = sum(Y.^(ii+jj), 3);
However as the expression Y.^(ii+jj) creates a 3D [M+1 x M+1 x 20] array it may need a large amount of memory that leads to decreased performance.
We know that x^(i+j) can be written as x^i * x^j So the expression can be written as:
result = sum(Y.^ii .* Y.^jj,3);
It has the same memory consumption as the previous method. But when we reach an expression that contains sum of products we should think about converting it to very fast matrix multiplication :
Z = X .^ jj; % A [M+1 x 20] matrix is created(implicit expansion)
result = Z * Z.' % multiply Z by its transpose
So the same result is obtained without the complexity of the other solutions.

Curve fitting of complex variable in Matlab

I want to solve the following system of equations shown in the image below,
The matrix system
where the component of the matrix A is complex numbers with the angle (theta) runs from 0 to 2*pi which has m divisions, and n = 9. The known value z = x + iy. Suppose the x and y of matrix z is
z =
0 1.0148
0.1736 0.9848
0.3420 0.9397
0.5047 0.8742
0.6748 0.8042
0.8419 0.7065
0.9919 0.5727
1.1049 0.4022
1.1757 0.2073
1.1999 0
1.1757 -0.2073
1.1049 -0.4022
0.9919 -0.5727
0.8419 -0.7065
0.6748 -0.8042
0.5047 -0.8742
0.3420 -0.9397
0.1736 -0.9848
0 -1.0148
How do you solve them iteratively? Notice that the value of the first component of the desired constants must equal 1. I am working with Matlab.
You can apply simple multilinear regression for complex valued data.
Step 1. Get the matrix ready for linear regression
Your linear system
written without matrices, becomes
that rearranged yelds
If you rewrite it with matrices you get
Step 2. Apply multiple linear regression
Let the system above be
where
Now you can apply linear regression, that returns the best fit for α when
where
is the conjugate transpose.
In MATLAB
Y = Z - A(:,1); % Calculate Y subtracting the first col of A from Z
R = A(:,:); R(:,1) = []; % Calculate R as an exact copy of A, just without first column
Rs = ctranspose(R); % Calculate R-star (conjugate transpose of R)
alpha = (Rs*R)^(-1)*Rs*Y; % Finally apply multiple linear regression
alpha = cat(1, 1, alpha); % Add alpha1 back, whose value is 1
or, if you prefer built-ins, have a look at regress function:
Y = Z - A(:,1); % Calculate Y subtracting the first col of A from Z
R = A(:,:); R(:,1) = []; % Calculate R as an exact copy of A, just without first column
alpha = regress(Y, R); % Finally apply multiple linear regression
alpha = cat(1, 1, alpha); % Add alpha1 back, whose value is 1

Using matlabs regress like polyfit

I have:
x = [1970:1:2000]
y = [data]
size(x) = [30,1]
size(y) = [30,1]
I want:
% Yl = kx + m, where
[k,m] = polyfit(x,y,1)
For some reason i have to use "regress" for this.
Using k = regress(x,y) gives some totally random value that i have no idea where it comes from. How do it?
The number of outputs you get in "k" is dependant on the size of input X, so you will not get both m and k just by putting in your x and y straight. From the docs:
b = regress(y,X) returns a p-by-1 vector b of coefficient estimates for a multilinear regression of the responses in y on the predictors in X. X is an n-by-p matrix of p predictors at each of n observations. y is an n-by-1 vector of observed responses.
It is not exactly stated, but the example in the help docs using the carsmall inbuilt dataset shows you how to set this up. For your case, you'd want:
X = [ones(size(x)) x]; % make sure this is 30 x 2
b = regress(y,X); % y should be 30 x 1, b should be 2 x 1
b(1) should then be your m, and b(2) your k.
regress can also provide additional outputs, such as confidence intervals, residuals, statistics such as r-squared, etc. The input remains the same, you'd just change the outputs:
[b,bint,r,rint,stats] = regress(y,X);

How to find a fitting function for [n x n] matrix values

Given 100x100 matrix where each element represents a function value in space, I would like to find parameter values A, B, C, D, E for a function f(x, y) = A + Bx + Cy + DX^2+Ey^2 that fits the best the given matrix values, where x represents a row number and y represents a column number
To illustrate the aim on a smaller example, let's say we have a 3x3 matrix T:
T = [0.1 0.2 0.1; 0.8, 0.6, 0.5; 0.1, 0, 1]
in this case f(1,1) = 0.1 and f(3,2)= 0.
Concretely the matrix values for which I would like to find a fitting function (surface) are displayed in the image below:
I would be very thankful if anyone suggested a way to find the 3D function that fits (best) the given matrix.
Edit
Is it possible to find a fit directly or is it neccesary (or better) fo first represent the data as matrix [X, Y, f(X,Y)]:
vals = []
for(i = 1: 100)
for j = 1 : 100
if(T(i,j) ~= 0)
vals = [vals;i, j, T(i,j)];
end;
end;
end;
These guys seemed to have done it in one line:
http://www.mathworks.com/matlabcentral/newsreader/view_thread/134076
x = % vector of x values
y = % vector of y values
z = % f(x,y)
V = [1 x y x.^2 x.*y y.^2];
a = V \ z ;
From the help page:
If A is a rectangular m-by-n matrix with m ~= n, and B is a column vector with m elements or a matrix with m rows, then A\B returns a least-squares solution to the system of equations A*x= B.