problem with my code please !!

Hi guys , How I can solve this error in my code see the attach file
the error appear in last four lines ...

8 Comments

Abdullah AlHarbi
Abdullah AlHarbi on 18 Mar 2016
Edited: Abdullah AlHarbi on 18 Mar 2016
the file is here ...
Your code ran for me without error (in R2016a).
What error did it throw when you ran it?
last four lines see it in command window
Ced
Ced on 19 Mar 2016
Edited: Ced on 19 Mar 2016
lol, it would be rather scary if he you see your command window. You could just post the error message... But see my answer below.
this is the error
Matrix dimensions must agree.
Error in MATLAB_ASSIMENTS (line 71) x=x1+x2+x3;
You need to create those vectors to be the same lengths. Consider using the linspace function.
Star Strider: is it possible that you are using the R2016a pre-release rather than the full version? There were some experimental changes in R2016a pre-release that were moved again before the full version.
Hi Walter,
I’m running the full R2016a. By mistake, I didn’t scroll all the way down, so I didn’t run the full code the first time. When I ran all of it, I got the error, and suggested linspace in the event the limits were described but the vector length was not. (We don’t know.) If it’s necessary to use the colon operator to create the vectors, and the limits are fixed, then I don’t see a solution.
We probably need to see the problem statement in order to attempt an Answer, which is the reason I didn’t.

Answers (2)

Interesting, not sure how matlab 2016 handles this. I am getting a "Matrix dimensions must agree" error, and for good cause.
In short:
You need to define n1, n2, n3 such that they all have the same length.
In detail:
in line 17: x1 has length 10
n1 = 6:15;
x1 = [ones(1, 10)]; % what you had
% x1 = 1:length(n1); % My suggestion
line 25/26: x2 has length 5
n2 = 1:5
x2 = (1/3).^n2
line 33/34: x3 has length 20
n3 = 1:20
x3 = cos (pi*n3/4)
In line 71, you try to add all three, but matlab does not know how to add three vectors of different lengths.
subplot has 3 indices. Changing your subplot indices as follows then it doesn't return error:
n=1:20
h=3*(-1/4).^n
;subplot(4,2,1);stem(n,h)
xlabel('Sample');ylabel('Amplitude');title('h[n]');
%(b) Calculate the output of this system for following inputs;
%1. x1[n] = u[n-5] 1 ? n ? 10
n1 = 6:15;
x1 = [ones(1, 10)];
subplot(4,2,2);stem(n1, x1)
xlabel('Sample');ylabel('Amplitude');title('x1[n]');
%2. x2[n] = (1/3)^n u[n] 1 ? n ? 5
n2 = 1:5
x2 = (1/3).^n2
subplot(4,2,3);stem(n2, x2)
xlabel('Sample');ylabel('Amplitude');title('x2[n]');
%3. x3[n] = cos (pi*n/4) 1 ? n ? 20
n3 = 1:20
x3 = cos (pi*n3/4)
subplot(4,2,4);stem(n3, x3)
xlabel('Sample');ylabel('Amplitude');title('x3[n]');
%The Output
%y1
y1 = conv(x1,h);
subplot(4,2,5);stem(y1)
xlabel('Sample');ylabel('Amplitude');title('y1');
%y2
y2 = conv(x2,h);
subplot(4,2,6);stem(y2)
xlabel('Sample');ylabel('Amplitude');title('y1');
%y3
y3 = conv(x3,h);
subplot(4,2,7);stem(y3)
xlabel('Sample');ylabel('Amplitude');title('y3');
If you find this answer of any help solving your question, please click on the thumbs-up vote link,
thanks in advance
John

This question is closed.

Tags

Asked:

on 18 Mar 2016

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!