if input 1 = x and input 2 = y, then output z is equal to ax + by + c.
What do the variables a, b and c correlate to?
Could these be something to do with each rules weight?
How do we calculate a, b and c?
What does that have to do with the data which we have in hand?
Related
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
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);
I am beginner. I have a matrix in matlab and I want Convert matrix's number to fuzzy number and use these fuzzy number for my function's input .how can I do this?
is it correct to Convert number to double number between 0,1 by Dividing numbers by 1000 like this?
[256,12;3,56]--->[0.256,0.12;0.003,0.056]
but for double number what should I do?
What do you mean by fuzzy number?!! as far as I know MATLAB uses normal numbers for Fuzzy system. After that there are fuzzifiers that change the real numbers to the points on the membership functions. And then the fuzzy logic decides that how the number have to be selected, and so on...!
On the other hand, If you want to change the scale of the number to be in the range [-1 1] or [0 1] then it has nothing to do with fuzzy.
and to change from the range [0 1] to [a b] use this line of code:
r = a + (b-a)*z;
where the z is in the range [0 1], and the r is in the range [a b]
for example, changing z=0.5 from [0 1] to the range [0 10], r becomes:
r = 0 + (10-0)*0.5 = 5
to change from [a b] to [0 1] also you can do this:
z = (r - a)/(b-a);
so if r = 5 in the range [0 10], then z = 0.5 in the range [0 1];
In addition, for the real fuzzy operation, try something like this:
point_n = 101; % Determines MF's resolution
min_x = -20; max_x = 20; % Universe is [min_x, max_x]
x = linspace(min_x, max_x, point_n)';
A = trapmf(x, [-10 -2 1 3]); % Trapezoidal fuzzy set A
B = gaussmf(x, [2 5]); % Gaussian fuzzy set B
C1 = fuzarith(x, A, B, 'sum');
subplot(2,1,1);
plot(x, A, 'b--', x, B, 'm:', x, C1, 'c');
title('fuzzy addition A+B');
C2 = fuzarith(x, A, B, 'sub');
subplot(2,1,2);
plot(x, A, 'b--', x, B, 'm:', x, C2, 'c');
title('fuzzy subtraction A-B');
C3 = fuzarith(x, A, B, 'prod');
That's how you perform fuzzy arithmetic. According to MathWorks:
Using interval arithmetic, C = fuzarith(X, A, B, operator) returns a fuzzy set C as the result of applying the function represented by the string, operator, which performs a binary operation on the sampled convex fuzzy sets A and B. The elements of A and B are derived from convex functions of the sampled universe, X:
A, B, and X are vectors of the same dimension.
operator is one of the following strings: 'sum', 'sub', 'prod', and
'div'.
The returned fuzzy set C is a column vector with the same length as
X.
And Finally you can perform fuzzy inference calculation using 'evalfis' function in MATLAB. The inputs and outputs to this function are real numbers as well:
fismat = readfis('tipper');
out = evalfis([2 1; 4 9],fismat)
This syntax generates the response
out =
7.0169
19.6810
I have an equation of the type c = Ax + By where c, x and y are vectors of dimensions say 50,000 X 1, and A and B are matrices with dimensions 50,000 X 50,000.
Is there any way in Matlab to find matrices A and B when c, x and y are known?
I have about 100,000 samples of c, x, and y. A and B remain the same for all.
Let X be the collection of all 100,000 xs you got (such that the i-th column of X equals the x_i-th vector).
In the same manner we can define Y and C as 2D collections of ys and cs respectively.
What you wish to solve is for A and B such that
C = AX + BY
You have 2 * 50,000^2 unknowns (all entries of A and B) and numel(C) equations.
So, if the number of data vectors you have is 100,000 you have a single solution (up to linearly dependent samples). If you have more than 100,000 samples you may seek for a least-squares solution.
Re-writing:
C = [A B] * [X ; Y] ==> [X' Y'] * [A';B'] = C'
So, I suppose
[A' ; B'] = pinv( [X' Y'] ) * C'
In matlab:
ABt = pinv( [X' Y'] ) * C';
A = ABt(1:50000,:)';
B = ABt(50001:end,:)';
Correct me if I'm wrong...
EDIT:
It seems like there is quite a fuss around dimensionality here. So, I'll try and make it as clear as possible.
Model: There are two (unknown) matrices A and B, each of size 50,000x50,000 (total 5e9 unknowns).
An observation is a triplet of vectors: (x,y,c) each such vector has 50,000 elements (total of 150,000 observed points at each sample). The underlying model assumption is that an observation is generated by c = Ax + By in this model.
The task: given n observations (that is n triplets of vectors { (x_i, y_i, c_i) }_i=1..n) the task is to uncover A and B.
Now, each sample (x_i,y_i,c_i) induces 50,000 equations of the form c_i = Ax_i + By_i in the unknown A and B. If the number of samples n is greater than 100,000, then there are more than 50,000 * 100,000 ( > 5e9 ) equations and the system is over constraint.
To write the system in a matrix form I proposed to stack all observations into matrices:
A matrix X of size 50,000 x n with its i-th column equals to observed x_i
A matrix Y of size 50,000 x n with its i-th column equals to observed y_i
A matrix C of size 50,000 x n with its i-th column equals to observed c_i
With these matrices we can write the model as:
C = A*X + B*Y
I hope this clears things up a bit.
Thank you #Dan and #woodchips for your interest and enlightening comments.
EDIT (2):
Submitting the following code to octave. In this example instead of 50,000 dimension I work with only 2, instead of n=100,000 observations I settled for n=100:
n = 100;
A = rand(2,2);
B = rand(2,2);
X = rand(2,n);
Y = rand(2,n);
C = A*X + B*Y + .001*randn(size(X)); % adding noise to observations
ABt = pinv( [ X' Y'] ) * C';
Checking the difference between ground truth model (A and B) and recovered ABt:
ABt - [A' ; B']
Yields
ans =
5.8457e-05 3.0483e-04
1.1023e-04 6.1842e-05
-1.2277e-04 -3.2866e-04
-3.1930e-05 -5.2149e-05
Which is close enough to zero. (remember, the observations were noisy and solution is a least-square one).
I have 2 matrices in Matlab, A and B, I am trying to find an easy way to take these in and output a function that maps A to B, it should be as easy as a function in the form of B=Ax+y where x and y are static numbers, but I cannot seem to remember my basic math skills today. Is there an easy way of doing this in Matlab?
Edit
This is the answer to the OP's original question as explained in the comments.
Take two elements b1 and b2 from B and the same elements a1 and a2 from A. Be sure that a1 ~= a2. If all elements of A are the same then the problem is trivial. Then compute
x = (b1-b2) / (a1-a2) ;
y = b1 - a1*x;
err = B - A*x - y;
total_error = sum(abs(err(:)));
If x and y do not satisfy the equation, then total_error > 0 and there is no such x and y.
Actually, if x and y are just numbers, you can just do
B = A*x + y;
Matlab is capable of doing matrix times scalar arithmetic by broadcasting the number x to each element of A.
If x is a vector and A*x makes sense, you can do the same.
If y is a scalar or a vector of the same size as A*x, you can also do the same.