keep getting errors on matlab

1 Ansicht (letzte 30 Tage)
Boss Man
Boss Man am 23 Feb. 2020
Kommentiert: Rena Berman am 14 Mai 2020
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 Kommentare
Steven Lord
Steven Lord am 23 Feb. 2020
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 am 14 Mai 2020
(Answers Dev) Restored edit

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Steven Lord
Steven Lord am 24 Feb. 2020
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.

Weitere Antworten (2)

the cyclist
the cyclist am 23 Feb. 2020
Instead of writing
sin(t)cos(t)
you need to put the explicit operations in, like this
sin(t)*cos(t)
  3 Kommentare
the cyclist
the cyclist am 23 Feb. 2020
Bearbeitet: the cyclist am 23 Feb. 2020
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 am 24 Feb. 2020
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.

Melden Sie sich an, um zu kommentieren.


Walter Roberson
Walter Roberson am 24 Feb. 2020
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 Kommentare
Walter Roberson
Walter Roberson am 24 Feb. 2020
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 am 25 Feb. 2020
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?

Melden Sie sich an, um zu kommentieren.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by