keep getting errors on matlab

조회 수: 1 (최근 30일)
Boss Man
Boss Man 2020년 2월 23일
댓글: Rena Berman 2020년 5월 14일
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
  댓글 수: 2
Steven Lord
Steven Lord 2020년 2월 23일
What is the full and exact text of the error and/or warning messages you receive, and which line throws the error or issues the warnings? Show us all the text displayed in the Command Window in red or orange exactly as it is displayed. The exact text may help us understand the cause and suggest a solution.
Rena Berman
Rena Berman 2020년 5월 14일
(Answers Dev) Restored edit

댓글을 달려면 로그인하십시오.

채택된 답변

Steven Lord
Steven Lord 2020년 2월 24일
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.

추가 답변 (2개)

the cyclist
the cyclist 2020년 2월 23일
Instead of writing
sin(t)cos(t)
you need to put the explicit operations in, like this
sin(t)*cos(t)
  댓글 수: 3
the cyclist
the cyclist 2020년 2월 23일
편집: the cyclist 2020년 2월 23일
You still have expressions like
y(i)sin(t(i))
where you have not corrected the first error I mentioned. That needs to be
y(i)*sin(t(i))
It is difficult to tell exactly what you are trying to calculate (and therefore exactly where the parentheses should go), but you have many expresions like
sum(cos(t(i))
that do not have paired parentheses. You have one extra open parenthesis. You need to fix all of those.
Also, FYI, it is better to post the code itself (like you did in your initial question, or with the paper clip icon). Posting an image of your code is not as helpful.
Walter Roberson
Walter Roberson 2020년 2월 24일
They did not say to replace sum(sin(t(i)) by sum(sint(i)) : they said that you are missing a ) on the sum() call.
Look more closely at your original code. You had
A=[N sum(sin(t(i)) sum(cos(t(i))
Count the bracket nesting level. At each point you have a ( increase the number by 1. At each ) decrease by 1:
A=[N sum(sin(t(i)) sum(cos(t(i))
00000111122332111112222334432
so by the time you get to the end of the line, you are missing two )
You also have the expression sin(t(i)) sum(cos(t(i)) with no operation between them. There are cases when you are inside [] like you are, in which space between values is valid and acts like a comma, such as
[sin(x) 3*x]
with that expression being the same as
[sin(x), 3*x]
However, space only separates elements in [] when you have no unclosed () relative to the most recent [] brackets started.
[sin(x([2 7])) 3*x]
The 2 and 7 are both not nested inside () relative to the [] so that is valid, and by the time of the 3*x the last ( has been closed with ) so the 3*x is validly separated by space. But in your code, when you encounter the second sum call, you are at () nesting level 1 relative to the most recent [], and space is not treated as an element separator when you have an unmatched () at that [] level. Your line
A=[N sum(sin(t(i)) sum(cos(t(i))
is not equivalent to
A=[N sum(sin(t(i)), sum(cos(t(i))
because doing so would act to make the second sum into the second parameter of the first sum call.

댓글을 달려면 로그인하십시오.


Walter Roberson
Walter Roberson 2020년 2월 24일
You need to rewrite a fair bit. In MATLAB, you cannot create an implicit summation with code along the line of
for i = 1 : 5
A = sum(t(i));
end
That would not result in A becoming the sum of t(1) through t(5) . Instead, the first round would extract t(1), call sum() passing that in, and sum() would see that it is being passed a scalar, so the return value from sum() would be the value itself, t(1) , and that value would overwrite all of A. Then in the second round when i = 2, the call would be sum(t(2)) and t(2) is a scalar, and sum() of a scalar is the same scalar, so A would be completely overwritten with t(2) . And so on. At the end, when i = 5, then A would be assigned sum(t(5)) which would be like A = t(5) . That would be the final value of A. Notice that no addition has been done!
Now, if you had
A = 0;
for i = 1 : 5
A = A + sum(t(i));
end
then you would have addition taking place. But the sum() calls are just useless there:
A = 0;
for i = 1 : 5
A = A + t(i);
end
  댓글 수: 4
Walter Roberson
Walter Roberson 2020년 2월 24일
You need to close the missing brackets and put in any missing multiplication signs for us to be able to figure that out.
Walter Roberson
Walter Roberson 2020년 2월 25일
t = xlsread('trandom.xlsx','B1:B58'); %time
y = xlsread('rtemp.xlsx','A2:A59'); %outdoor temperature
N=57;
tN = t(1:N);
yN = y(1:N);
A=[N, sum(sin(tN)) ,sum(cos(tN))
sum(sin(tN)), sum(sin(tN).*sin(t(57)) ,sum(sin(tN)*cos(tN))
sum(cos(tN)), sum(sin(tN).*cos(tN)) ,sum(cos(tN).*cos(tN))];
B=[sum(yN)
sum(yN.*sin(tN))
sum(yN.*cos(tN))];
end
X=A\B
However, I recommend that you pay attention to Steven Lord's recommended optimization of calculating the sine and cosine terms only once each.
Question: if you are not going to use all 58 entries that you read, then why read all of them?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by