discretise domain of x and y with 0.01 separation between points - matlab

I am looking to calculate an array from a formula using x and y variables, the domain of x is (0,50) and y is (0,30) . I am asked to discretise the domain of x and y with 0.01 separation between points, then compute L(x,y) (which I have a formula for)(This will be points of a graph, ultimately I'm looking for the min lengths between points)
I'm not sure what I need to define in my script because if I define x and y as arrays with 0.01 separation they end up being uneven and unable to calculate as the arrays are uneven
%change these values for A, B and C positions
Ax=10;
Ay=5;
Bx=15;
By=25;
Cx=40;
Cy=10;
x = 0:0.01:50; % Array of values for x from 0-50 spaced at 0.01
y = 0:0.01:30; % Array of values for y from 0-30 spaced at 0.01
%length of point P from A, B and C and display
Lpa=sqrt((Ax-x).^2+(Ay-y).^2);
Lpb=sqrt((Bx-x).^2+(By-y).^2);
Lpc=sqrt((Cx-x).^2+(Cy-y).^2);
L=Lpa+Lpb+Lpc
I am getting an error telling me the two matrix are not even which makes sense to not work but I'm not sure how to define a matrix that will result in the minimum x and y values I am after.
Any help would be greatly appreciated.

You want to calculate L for each possible pair of x and y. In other words, for the first value of x = 0, you will calculate L for all y values from 0 to 30, then for next value of x = 0.01, you will do the same and so on.
MATLAB has a really cool function called meshgrid to create a matrix for every pair of x and y. So after generating x and y, change your code to the following to get a 2D matrix for L -
[X, Y] = meshgrid(x, y)
%length of point P from A, B and C and display
Lpa = sqrt((Ax - X).^2 + (Ay - Y).^2);
Lpb = sqrt((Bx - X).^2 + (By - Y).^2);
Lpc = sqrt((Cx - X).^2 + (Cy - Y).^2);
L = Lpa + Lpb + Lpc

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: Plot 3D surface from irregular data points

Let's say I have a vector of x coordinates, a matrix of y coordinates and corresponding z values:
xcoordinates = [1 2 3 4 5];
ycoordinates = repmat(xcoordinates,5,1)+rand(5,5);
z = zeros(5,5);
for x=xcoordinates
for y=1:5
z(x,y) = sqrt(x^2+ycoordinates(x,y)^2);
end
end
How do I plot a surface determined by the z values at the points given by the corresponding x and y values? The first x value defines the x value for all y values in the first row of the matrix, the second x value to all values in the second row and so on.
(If the answer is griddata I would like some additional pointers. How can I get my data into the correct format?)
mesh(repmat(xcoordinates,5,1), ycoordinates, z)
By the way, you could easily vectorize this computation:
x = repmat(1:5, 5, 1);
y = x + rand(5,5);
z = sqrt(x.^2+y.^2);
mesh(x', y, z)

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 integrate matrices (Sum of Matrices with dx spacing) in MATLAB?

I'm pretty confused on how I would go about summing an infinite amount of matrices in MATLAB. Lets say I have this function (a gaussian):
%Set up grid/coordinate system
Ngrid=400;
w=Ngrid;
h=Ngrid;
%Create Gaussian Distribution
G = zeros ([w, h]);
Sig = 7.3; %I want the end/resultant G to be a summation of Sign from 7.3 to 10 with dx
for x = 1 : w
for y = 1 : h
G (x, y) = exp (-((Sig^-2)*((x-w/2+1)^2 + (y-h/2+1)^2)) / (2));
end
end
I essentially want the end/resultant function G to be a summation of Sign from 7.3 to 10 with dx (which is infinitesimally) small ie integration. How would I go about doing this? I am pretty confused. Can it even be done?
You don't appear to actually be summing G over a range of Sig values. You never change the value of Sig. In any case, assuming that dx isn't too small and that you have the memory this can be done without any loops, let alone two.
Ngrid = 400;
w = Ngrid;
h = Ngrid;
% Create range for Sig
dx = 0.1;
Sig = 7.3:dx:10;
% Build mesh of x and y points
x = 1:w;
y = 1:h;
[X,Y] = meshgrid(x,y);
% Evaluate columnized mesh points at each value of Sig, sum up, reshape to matrix
G = reshape(sum(exp(bsxfun(#rdivide,-((X(:)-w/2+1).^2+(Y(:)-h/2+1).^2),2*Sig.^2)),2),[h w]);
figure
imagesc(G)
axis equal
This results in a figure like this
The long complicated line above can be replaced by this (uses less memory, but may be slower):
G = exp(-((X-w/2+1).^2+(Y-h/2+1).^2)/(2*Sig(1)^2));
for i = 2:length(Sig)
G = G+exp(-((X-w/2+1).^2+(Y-h/2+1).^2)/(2*Sig(i)^2));
end

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.