5 views (last 30 days)

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

Steven Lord
on 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.

Sign in to comment.

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)

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

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.

Sign in to comment.

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

Walter Roberson
on 24 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

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?

Sign in to comment.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 2 Comments

## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/507126-keep-getting-errors-on-matlab#comment_801202

⋮## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/507126-keep-getting-errors-on-matlab#comment_801202

## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/507126-keep-getting-errors-on-matlab#comment_847333

⋮## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/507126-keep-getting-errors-on-matlab#comment_847333

Sign in to comment.