Fit a Fourier Model with zero a0 - matlab

following code finds coefficients in Fourier series
load enso;
f = fit(month,pressure,'fourier2');
output of the code:
General model Fourier1:
f(x) = a0 + a1*cos(x*w) + b1*sin(x*w)
Coefficients (with 95% confidence bounds):
a0 = 10.63 (10.22, 11.03)
a1 = 2.876 (2.187, 3.565)
b1 = 1.177 (0.09191, 2.261)
w = 0.5263 (0.5225, 0.5301)
I need to find coefficients for function f(x) = a1*cos(x*w) + b1*sin(x*w), so a0 should be either 0 or should not be in the formula at all. How to do this?

Do you want 'fourier1' or 'fourier2'? Because you have both in your question. You can limit a0 to 0 by using fit options. Below are the fits for both, the -Inf to Inf are ranges for the a's, b's and w while the 0 to 0 is limiting a0.
load enso;
ft1 = fittype('fourier1');
options1 = fitoptions(ft1);
options1.Lower = [0 -Inf -Inf -Inf];
options1.Upper = [0 Inf Inf Inf];
f1 = fit(month,pressure,ft1,options1)
ft2 = fittype('fourier2');
options2 = fitoptions(ft2);
options2.Lower = [0 -Inf -Inf -Inf -Inf -Inf];
options2.Upper = [0 Inf Inf Inf Inf Inf];
f2 = fit(month,pressure,ft2,options2)
Edit:
It runs fine for me. That's odd...
f1 =
General model Fourier1:
f1(x) = a0 + a1*cos(x*w) + b1*sin(x*w)
Coefficients (with 95% confidence bounds):
a0 = 0 (fixed at bound)
a1 = 2.258 (-1.631, 6.148)
b1 = 2.406 (-1.317, 6.13)
w = 0.5311 (0.516, 0.5462)

Related

how to find all the possible intersections between two vectors with such organization in matlab

I have a matrix y and a vector x, I need to find all the possible vectors resulted from the mapping of each value in x into each vector in y.
That is difficult to be understood; let's explain is with an example:
Here is an example, I have the vector x = [0.7 + 0.7i; 0.7-0.7i]; The matrix y = [1 0; 2 0; 1 2]; the resulted matrix is supposed to be like this one Z = [0.7 + 0.7i 0; 0.7-0.7i 0; 0 0.7 + 0.7i; 0 0.7-0.7i; 0.7 + 0.7i 0.7 + 0.7i; 0.7 + 0.7i 0.7-0.7i; 0.7 - 0.7i 0.7-0.7i ; 0.7 - 0.7i 0.7+0.7i]; . That is equivalent into Z = [x_1 0; x_2 0; 0 x_1; 0 x_2; x_1 x_1; x_1 x_2; x_2 x_2; x_2 x_1];. That means it map each value in x into the row of Z according to the index value in y.
Here is my try code:
clear all; clc;
y = [];
G = 2;
v = 1 : G;
for i = 1: G
x=nchoosek(v,i);
m = zeros(size(x,1),G-i);
y =[y ; x m]; % creat the matrix y
end
x = [0.7 + 0.7i; 0.7-0.7i];
Z = []; s = zeros(G,1);
for k=1:size(x,1)
for i=1:size(y,1)
n=y(i,:);
n=n(n ~= 0);
s(n)=x(k);
Z=[Z s];
s = zeros(G,1);
end
end
The problem in my code that matrix Z show the inverse, it means it takes the input x_1 from x and then map it into all possible values in y. For example the matrix Z starts with [x_1 0; 0 x_1; x_1 x_1 ….], however that should be the inverse, which means takes each values in x and map it as shown in the above example [x_1 0; x_2 0; x_3 0 …..]. The second issue, when y contains more than non-zeros values, my code cannot get all possible vectors, it can only get [x_1 x_1; x_2 x_2]; but I cannot map the other possibilities which are [x_1 x_2; x_2 x_1] and so on.
How can I solve that issue?
UPDATE
Here is the updated question with clear description. I have the vector x and matrix y, I need to fill the matrix z following the index taken from each row in matrix y. For example, if the first row in matrix y is [1 0] or [0 1]; then I will take all possible values from x and put it in z following the number taken from the row in y which is 1 in this case. Then, the same case for row 2 in matrix y which is [2 0] or [0 2]; it means that second column in z will be filled with all possible values in x.
Then, the two columns in z can be filled which is equivalent into the case [1 2] in y, so it will take the first value from x and fill it with all other possible values from x, and so on. The rows in z should not be repeated.
The matrix Z is exactly as shown with below answer of AboAmmar below, but using the loop if with longer vector x and bigger matrix y will be little bit complicated.
As you describe it, there are 4 distinct cases for each row of y and the corresponding output:
[0 1] or [1 0] => [x 0]
[0 2] or [2 0] => [0 x]
[1 2] => [x1 x1; x1 x2; x2 x2; x2 x1]
[2 1] => [x1 x1; x2 x1; x2 x2; x1 x2]
These don't seem to follow any obvious rule. So, the easiest (but not smartest) solution is to use if-else and select the suitable case from the above. We don't have all the information about the possible indices, or if rows like [1 1] and [2 2] might happen, so the following solution is by no means exhaustive; surprising errors might happen if other inputs are fed into y matrix.
y = [];
G = 2;
v = 1 : G;
for i = 1: G
x = nchoosek(v,i);
m = zeros(size(x,1),G-i);
y = [y ; x m]; % creat the matrix y
end
Z = [];
x = [0.7 + 0.7i; 0.7-0.7i]
for i = 1:size(y,1)
r = y(i,:);
if ismember(r, [1 0; 0 1], 'rows')
Z(end+1:end+2,:) = [x [0; 0]];
elseif ismember(r, [2 0; 0 2], 'rows')
Z(end+1:end+2,:) = [[0; 0] x];
elseif ismember(r, [1 2], 'rows')
Z(end+1:end+4,:) = [x(1) x(1); x(1) x(2); x(2) x(2); x(2) x(1)];
elseif ismember(r, [2 1], 'rows')
Z(end+1:end+4,:) = [x(1) x(1); x(2) x(1); x(2) x(2); x(1) x(2)];
end
end
Z =
0.7000 + 0.7000i 0.0000 + 0.0000i
0.7000 - 0.7000i 0.0000 + 0.0000i
0.0000 + 0.0000i 0.7000 + 0.7000i
0.0000 + 0.0000i 0.7000 - 0.7000i
0.7000 + 0.7000i 0.7000 + 0.7000i
0.7000 + 0.7000i 0.7000 - 0.7000i
0.7000 - 0.7000i 0.7000 - 0.7000i
0.7000 - 0.7000i 0.7000 + 0.7000i
Your code is valid if you have fix length in y, for example if each vector in y has one value and others are zeros, or two non-zeros values ...etc.
So you can do your code for each length separately and then build the matrix Z by combining all other matrices.

How to find transformation matrix from the output with Gaussian noise?

For the below given input and output, the matrix A can be found out by pseudoinverse or mrdivision in MATLAB. Similarly, I would now like to know, how to determine A, if my output signal Y matrix contains additive zero mean, uncorrelated, Gaussian noise?
x1 = [1 1 1]';
x2 = [0 1 1]';
x3 = [0 0 1]';
x4 = [1 0 1]';
y1 = [1 2 0]';
y2 = [-1 0 3]';
y3 = [3 1 1]';
y4 = [5 3 -2]';
X = [x1 x2 x3 x4];
Y = [y1 y2 y3 y4];
A = Y/X
Also, I have modelled the unknown noisy output as below:
y1_n = y1 + sqrt(var(y1))*randn(size(y1));
y2_n = y2 + sqrt(var(y2))*randn(size(y2));
y3_n = y3 + sqrt(var(y3))*randn(size(y3));
y4_n = y4 + sqrt(var(y4))*randn(size(y4));
Y = [y1_n y2_n y3_n y4_n];
The statement A = Y/X solves the linear system of equations A*X = Y. If the system is overdetermined, as in your case, the solution given is the least squares solution. Thus, if you have additive, zero mean, uncorrelated, Gaussian noise on Y, then A = Y/X will give you the best possible, unbiased, estimate of A.
Note that the noise you add to your Y matrix is quite large, hence the estimate of A is far away from the ideal. If you add less noise, the estimate will be closer:
x1 = [1 1 1]';
x2 = [0 1 1]';
x3 = [0 0 1]';
x4 = [1 0 1]';
X = [x1 x2 x3 x4];
y1 = [1 2 0]';
y2 = [-1 0 3]';
y3 = [3 1 1]';
y4 = [5 3 -2]';
Y = [y1 y2 y3 y4];
for n = [1,0.1,0.01,0]
Y_n = Y + n*randn(size(Y));
A = Y_n/X;
fprintf('n = %f, A = \n',n)
disp(A)
end
Output:
n = 1.000000, A =
2.9728 -5.5407 2.8011
2.6563 -1.3166 0.6596
-3.3366 1.1349 1.5342
n = 0.100000, A =
2.0011 -4.0256 2.9402
1.9223 -1.0029 1.0921
-3.1383 1.9874 1.0913
n = 0.010000, A =
1.9903 -3.9912 2.9987
1.9941 -1.0001 1.0108
-3.0015 2.0001 1.0032
n = 0.000000, A =
2.0000 -4.0000 3.0000
2.0000 -1.0000 1.0000
-3.0000 2.0000 1.0000
Of course if you make X and Y larger by adding more vectors you'll get a better estimate too, and will be able to compensate more noisy data.

large number of linear equations in MATLAB

I want to use solve() to solve a large system of linear equations. The solve() function needs equations and variables. I use a loop to generate the equations and my variables are contained in a large array. This is a simple code of what I am trying to do:
x = sym('x',[1 3])
eqn = sym('eqn', [1,3])
eqn1 = 2*x1 + x2 + x3 - 2 == 0
eqn2 = 2*x2 -x2 -x1- x3 == 3
eqn3 = 2*x2+ x1 + 3*x3 == -10
Y = solve(eqn, x)
MATLAB does not recognize my variable x1. I have solved the same system using the following code:
syms x1 x2 x3
eqn1 = 2*x1 + x2 + x3 == 2
eqn2 = 2*x2 -x2 -x1 - x3 == 3
eqn3 = 2*x2+ x1 + 3*x3 == -10
X = solve([eqn1, eqn2, eqn3, eqn4], [x1 x2 x3])
structfun(#subs,X)
But this is useless for a very large number of equations. What am I doing wrong?
You don't need symbolic (syms) for that. This is a standard linear system of equations that can be represented as:
Ax = b where A = [2 1 1; -1 1 -1; 1 2 3], x = [x1; x2; x3] and b = [0; 3; -10]
To solve for x, you would first define
A = [2 1 1; -1 1 -1; 1 2 3]
and
b = [0; 3; -10]
and then solve using
x = A\b
PS. There are some odd things in your question, eg. in eq.2 eqn2 = 2*x2 -x2 -x1- x3 == 3 I assume you omitted simplying this to -x1 +x2 -x3 ==3.
PS2. This is pretty standard Matlab, you can find a lot of info under the standard mldivide page in the documentation along with a lot similar questions here on SO.

Solving a Bidiagonal System in MATLAB

I have a matrix of the following form:
d1 0 0 0 0 0 0 x1 b1
a1 d2 0 0 0 0 0 x2 b2
0 a2 d3 0 0 0 0 x3 b3
0 0 a3 d4 a4 0 0 * x4 = b4
0 0 0 0 d5 a5 0 x5 b5
0 0 0 0 0 d6 a6 x6 b6
0 0 0 0 0 0 d7 x7 b7
I need to write a code to solve this system without forward elimination. I was assuming you could just solve for x7 = b7/d7. Then solve for d6 by setting d6 = b6-a6*x7 = b6-a6*(b7/d7) from our first iteration... then carrying the rest backwards to the first row. However, I am experiencing a block:
1) How to hardcore the confusing differences in indices
2) How to start from a reverse order down from d7 up to the first equation
3) Moreover, how do I generalize this for any system of n? According to personal readings I am doing for recreation, n is odd.
Can anyone assist me in coding this?
EDIT: The coefficient matrix is A, the x column vector can be denoted as X, and the b column vector can be denoted as B for convenience if needed.
Let A be your n-by-n bi-diagonal matrix and B your right-hand-side of the equation. You can solve using this loop:
n = size(A,1);
x = zeros(n,1); %// pre-allocate for solution x
x(n) = B(n)/A(n,n); %// get the last element
for ii=(n-1):-1:1
x(ii) = (B(ii)-x(ii+1)*A(ii,ii+1))/A(ii,ii); %// iteratively solve
end
Of course, it is always best to use built-in functions
x = A\B;

Solving a system of matrix equations using MATLAB?

I have a system of three equations that I'd like to solve via MATLAB, and I'm a bit confused on how to go about doing it.
I have three equations:
A = R*P1
B = R*P2
C = R*P3
A, B, C, and P1, P2, and P3 are 3x1 matrices, while R is a 3x3 matrix. R is the same for all three equations.
I need to find R, and am given A, B, C, and the P's.
I wanted to use fsolve, but it seems that fsolve doesn't work when the variables are matrices. What is an alternative method that you would recommend using?
Just making up some numbers to work with:
P1 = [1;1;1];
P2 = [2;3;4];
P3 = [5;4;3];
R = [2 4 5; 1 5 4; 1 2 3];
Which would mean that:
A = [11;10;6];
B = [36;33;20];
C = [41;37;22];
If A, B, C, P1, P2, P3 are all numerical, why don't you simply use the ldivide or \ operator? This will allow you to solve your linear system directly. I can see that you have the following relationships:
R*P1 = A
R*P2 = B
R*P3 = C
You can see that each matrix equation yields three constraints. What you can do is create one system that encapsulates all matrix equations together thus yielding 9 constraints. As such, you'll need to reformulate this to be able to solve for your coefficients in your matrix R differently. To do this, we need to reshape your matrix R such that it becomes a 9-element vector. In other words, we can then reformulate your system like so:
[P1 0 0 0 0 0 0] [R1] [ ]
[0 0 0 P1 0 0 0] [R2] [ A ]
[0 0 0 0 0 0 P1] [R3] [ ]
[P2 0 0 0 0 0 0] [R4] [ ]
[0 0 0 P2 0 0 0] * [R5] = [ B ]
[0 0 0 0 0 0 P2] [R6] [ ]
[P3 0 0 0 0 0 0] [R7] [ ]
[0 0 0 P3 0 0 0] [R8] [ C ]
[0 0 0 0 0 0 P3] [R9] [ ]
P * R = D
You'll see that we have a 9 x 9 matrix called P, our matrix R which is reshaped into a vector so that we can solve for the coefficients and D are A,B,C concatenated together into one vector. R1 to R9 are the coefficients of the matrix R that are read from left to right and top to bottom.
Therefore, to find your coefficients in your matrix, simply do:
R = P^{-1}*D
As such, simply construct the matrix P and vector D like so:
P = [P1.' zeros(1,6); zeros(1,3) P1.' zeros(1,3); zeros(1,6) P1.'; ...
P2.' zeros(1,6); zeros(1,3) P2.' zeros(1,3); zeros(1,6) P2.'; ...
P3.' zeros(1,6); zeros(1,3) P3.' zeros(1,3); zeros(1,6) P3.'];
D = [A; B; C];
Now, simply solve for R and reshape it back into a 3 x 3 matrix. Therefore:
R = P \ D;
R = reshape(R, 3, 3).';
reshape turns our vector into a 3 x 3 matrix, but it constructs the matrix in column-major format, so you need to transpose the result after you call reshape. With your example, this is what we get. I construct P1, P2, P3, A, B, C, then use the code I had from before:
P1 = [1;1;1];
P2 = [2;3;4];
P3 = [5;4;3];
A = [11;10;6];
B = [36;33;20];
C = [41;37;22];
P = [P1.' zeros(1,6); zeros(1,3) P1.' zeros(1,3); zeros(1,6) P1.'; ...
P2.' zeros(1,6); zeros(1,3) P2.' zeros(1,3); zeros(1,6) P2.'; ...
P3.' zeros(1,6); zeros(1,3) P3.' zeros(1,3); zeros(1,6) P3.'];
D = [A; B; C];
R = P \ D;
R = reshape(R, 3, 3).';
To verify that R is correct, do:
A1 = R*P1;
B1 = R*P2;
C1 = R*P3;
We get for each:
A1 =
11
10
6
B1 =
36
33
20
C1 =
41
37
22
This matches up with your example. However, note that you may get a warning that R is ill-conditioned. This is because you may not have enough constraints to properly find a unique inverse. You may have to add more constraints to get a unique inverse, but if you can't, then use this with caution.
Have you tried to flatten out the matrices inside of the solve function? There is a collect function in MATLAB that collects coefficients, but you need to check if it works with supplying a row matrix, as opposed to a sum. (maybe sum(row matrix) - if it also combines like terms)
Something like:
[x,y,x] = solve(sum(R*P1)==A, sum(R*P2)==B, sum(R*P3)==C, x,y,z)
or the fsolve function