0

I am trying to plot the following function:

enter image description here

The time interval is to be from 0 to 5 seconds using a step size of 0.002. This is what I have so far...

t = 0:0.002:5;
k = [2; 4; 6; 8];
i = (1/pi) + 0.5*sin(4*t) - (2/pi)*sum((cos(4*k*t))/(k*k-1));
plot(t,i)

It gives me the error:

Error using  * 
Inner matrix dimensions must agree.

Error in lab1_5 (line 4)
i = (1/pi) + 0.5*sin(4*t) -
(2/pi)*sum((cos(4*k*t))/(k*k-1));

I then tried every use of './' and '.*' but it still gives me the same error. What am I doing wrong?

0

1 Answer 1

1

Change your code to:

t = 0:0.002:5;
k = [2; 4; 6; 8];

i = (1/pi) + 0.5*sin(4*t)
for j=1:4
    i = i-(2/pi)*((cos(4*k(j)*t))/(k(j)*k(j)-1));
end
plot(t,i)

The reason is that k is a col-vector (or a 4x1 matrix), so you cannot simply multiply it by k*k. For matrix multiplication, the size of left and right must follow n × m and m × p. In this case, you need to loop multiply every elements of k.

Sign up to request clarification or add additional context in comments.

4 Comments

You are not handling the multiple indexing at all in your answer. k'*k is a constant! That whacks the sum.
Ah that's it, thank you. Just wondering, if k is a row vector, what then should I do?
You're getting closer. The size of your summands is 1,1, not 1,2501 which is how you're treating it. You don't need the "sum" term, that's whacking your results (still)
@John Yeah, too careless for me. Fixed and thanks. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.