MATLAB: Keep getting errors on matlab

MATLAB

t = xlsread('trandom','E2:E59');
y = xlsread(rtemp.xlsx','H2:H59');
N=58;
for i=1:N
A=[N sum(sin(t(i)) sum(cos(t(i))
sum(sin(t(i)) sum(sin(t(i))*sin(t(i)) sum(sin(t(i))cos(t(i))
sum(cos(t(i)) sum(sin(t(i))cos(t(i)) sum(cos(t(i))*cos(t(i))];
B=[sum y(i)
sum(y(i)sin(t(i))
sum(y(i)cos(t(i))];
end
X=A\B
idea is to solve ax=b for x, where ive written sum= summation of each i term of t or y
keep getting errors, not confident with matlab
Thanks for any help given

Best Answer

Let's define a few new variables. But first, here are the definitions of the three original variables from your code:
t = xlsread('trandom','E2:E59');
y = xlsread(rtemp.xlsx','H2:H59');
N=58;
Rather than calling sin and cos repeatedly on individual elements of the vector t, I'll call them once on the whole vector. I can then use those vectors to define the terms that are the product of the sines and cosines, taking advantage of array (element-wise) multiplication.
st = sin(t);
ct = cos(t);
sst = st.*st; % sin(t).*sin(t)
sct = st.*ct; % sin(t).*cos(t)
cct = ct.*ct; % cos(t).*cos(t)
% We don't need cst as sct (which is sin(t).*cos(t)) is the same as cos(t).*sin(t)
% but if you want the symmetry
cst = ct.*st;
Now, looking at the pattern for A, it looks like we have:
A = [ N, sum(st), sum(ct); ...
sum(st), sum(sst), sum(sct); ...
sum(ct), sum(sct), sum(cct)];
I'm assuming the variable t is a vector. If it's not, you can either make it a vector before defining the variables st and ct or you can (if you're using a sufficiently new release) specify that you want to sum over 'all' the dimensions of the variable.
A = magic(3)
s1 = sum(A) % sum of the columns
sA = sum(A, 'all') % Older releases can use sum(A(:))
You can do something similar definining variables for the combinations of y and functions of t, and use those to define b.