Error with multiplication, it saids that matrix dimensions must be equal, How do I multiply in my formulas?

4 views (last 30 days)
Every time I run the program, I get an error when the program calculates Vc. The error states the matrix dimensions must agree, I was wondering how to fix this so my formula works correctly. Error still happens when I remove the dot from multiplication sign. I've asked my professor but he doesn't recognize why the error is happening. Respond ASAP please, as I was given this friday and the report is due sunday.
%time the switch is closed for
t = 0:1:10;
%User enters the values for E, R, C.
e = exp(-t/R*C);
dvc = E - VcO; %Vc then letter "O"
%Formulas for Vc, ic, Vr
Vc = E + ((VcO - E).* e);
ic = (dvc/R).*e;
Vr = dvc .* e;

Accepted Answer

John D'Errico
John D'Errico on 9 Nov 2014
Edited: John D'Errico on 9 Nov 2014
You have not given us sufficient information to know anything about your problem.
You did not tell us what VcO is. Is it a number, a scalar? Is it a vector? WHAT IS IT?
My guess is either:
- VcO is causing the problem, that you have defined it somehow as a vector or matrix of some unknown size, and you do not realize that, or...
- You tell us that E, R , and C are entered by the user, but you do not show us HOW they are input. Too often when I see this done by a novice, they do not realize that they have entered CHARACTER versions of a number. For example, '123' is NOT the number 123 in MATLAB. It is an ascii representation, composed of 3 bytes of information, not the double precision number 123.
The point I am making is that often when novice enters a number, they do not realize that in fact, they entered a character representation of that number. In that case, that variable will be an ascii string, but not a number itself. It will be a vector, so when you try to multiply one vector by another, matlab gets confused when one of them is a string, and a vector of the wrong size! This could explain the error message that you got.
So again, I don't know, nor can I know what you have done wrong. And when code LOOKS reasonable as it is written, then that almost always means that one or more of the variables is not as you tell us it is, not what you think the variable is. So that is what you must check.
You can fix it trivially though, by carefully looking at each variable in this block of code. Execute each line at a time. Look at what you get. Is it the size and shape you expect? Use the functions who and whos. The workspace tab will also tell you this information. What do they tell you about your variables? Are the numbers as you expect to see? If not, where does the error occur?
If necessary, respond by showing the result of whos. Are all the variables that you THINK are scalars really scalar variables? Are they all of double class?
Learn to debug your code, by learning to look critically at each line, at each variable. Does the code do what it should do?
  2 Comments
Adam
Adam on 9 Nov 2014
You were right. I found out what the problem was in my code, I forgot to put 'input (' for my variable VcO. Thanks for replying.
Adam
Adam on 9 Nov 2014
I looked at someone's code in my class and realized the mistake was with VcO variable. Thanks for taking your time to reply; I just need to get used to MATLAB syntax as I been coding with Java and C++ for so long.

Sign in to comment.

More Answers (1)

Adam
Adam on 9 Nov 2014
I found out what the problem was in my code. I forgot to place 'input(' for one of my variables (VcO) where I as asking the value from the user.
Thanks for you all taking time out of your day to trying to solve my problem, your advice was really appreciated.

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!