keep getting errors on matlab
Show older comments
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 Comments
Steven Lord
on 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
on 14 May 2020
(Answers Dev) Restored edit
Accepted Answer
More Answers (2)
the cyclist
on 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 Comments
the cyclist
on 23 Feb 2020
Edited: the cyclist
on 23 Feb 2020
Can you post all of your new version of the code, and the complete error message?
It would be ideal if you also posted the input files (using the paper clip icon), so that we can just run your code ourselves and see what is going wrong.
the cyclist
on 23 Feb 2020
Edited: the cyclist
on 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
on 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.
Walter Roberson
on 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 Comments
Boss Man
on 24 Feb 2020
Walter Roberson
on 24 Feb 2020
Edited: Walter Roberson
on 25 Feb 2020
No. sum(t(i)) is currently only calculating the sum of the current t(i) together with nothingness, getting out the current t(i) value. There is no meaningful summation in your current code, so it is meaningless to talk about "still" computing a summation.
The paragraph I showed "Now, if you had" does not use summation in the loop: it uses addition to calculate a running total of the values seen so far.
I realized last night that maybe what you were trying to do was
sum(sin(t))
With no indexing, or equivalently,
sum(sin(t(1:N))
If so then you would not use a for loop.
Walter Roberson
on 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
on 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?
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!