ok, for my first practice homeowrk, since i have no idea what i am doing in matlab, i was asked to plot the response of a RLC equation that is in the time domain. I got the correct answer, but im not sure what the syntax is doing and if it is needed.
q0=10;R=60;L=9;C=0.0005;
t=linspace(0,0.8)
q=q0*exp(-R*t/(2*L)).*cos(sqrt(1/(L*C)-(R/(2*L))^2)*t);
plot(t,q)
in the equation q, there is the .* operation. Is that even needed if a matrix was not even used? im confused as when to use .* vs *
i know if i had
a=[1 2 3]
b=[5 6 7]
a.*b
would return
5 12 21
but is .* needed in my above q equation?
thanks

 Réponse acceptée

"but is .* needed in my above q equation?"
Yes.
t is a vector, and q0, R, L, and C are all scalars.
q0=10;R=60;L=9;C=0.0005; % scalars
t=linspace(0,0.8); % 1-by-100 vector
Consider the expressions to the left and to the right of the .*, which both include t and some scalars:
q0*exp(-R*t/(2*L)) % vector the size of t
ans = 1×100
10.0000 9.7342 9.4755 9.2237 8.9786 8.7400 8.5077 8.2816 8.0615 7.8472 7.6387 7.4357 7.2381 7.0457 6.8584 6.6762 6.4987 6.3260 6.1579 5.9942 5.8349 5.6799 5.5289 5.3820 5.2389 5.0997 4.9642 4.8323 4.7038 4.5788
cos(sqrt(1/(L*C)-(R/(2*L))^2)*t) % vector the size of t
ans = 1×100
1.0000 0.9931 0.9726 0.9386 0.8917 0.8326 0.7620 0.6808 0.5904 0.4917 0.3864 0.2757 0.1612 0.0444 -0.0729 -0.1892 -0.3029 -0.4125 -0.5164 -0.6131 -0.7015 -0.7801 -0.8480 -0.9043 -0.9481 -0.9788 -0.9961 -0.9996 -0.9894 -0.9655
They are both vectors the size of t, so when you multiply those two vectors, you want to do it element-wise, which is what .* does
q=q0*exp(-R*t/(2*L)).*cos(sqrt(1/(L*C)-(R/(2*L))^2)*t)
q = 1×100
10.0000 9.6672 9.2155 8.6574 8.0065 7.2767 6.4825 5.6385 4.7592 3.8588 2.9513 2.0497 1.1664 0.3131 -0.5000 -1.2633 -1.9688 -2.6095 -3.1798 -3.6753 -4.0929 -4.4309 -4.6887 -4.8668 -4.9668 -4.9916 -4.9446 -4.8303 -4.6538 -4.4210
giving you another vector the size of t.

9 commentaires

Matt Thomas
Matt Thomas le 13 Mai 2022
interesting. please forgive me for the moment as i still try and understand it.
why do we need it to be a element by element multiplication?
is it because, if we were to do this by hand, and plug in for t, we would want each time value in the same place holder to be multiplied?
ie 10 adnd 1 share the same place holder so multiply them out and 9.7 with .9 9.4 with .97 etc etc
thanks
"10 adnd 1 share the same place holder so multiply them out and 9.7 with .9 9.4 with .97 etc etc"
Exactly.
You want to calculate q for each t here:
q=q0*exp(-R*t/(2*L)).*cos(sqrt(1/(L*C)-(R/(2*L))^2)*t);
and you don't want any element of the result q to come from any "mixing" of different elements of t - it wouldn't make sense. But that's what matrix multiplication (*) would do if you use it, by the definition of matrix multiplication. Instead, you want all the elements of t to be used independently of each other, so that's element-wise (.*), as you describe.
[For scalars being multiplied by anything (i.e., another scalar, a vector, a matrix, or even a higher-dimensional array), it doesn't matter if you use * or .* because there's only one thing it can mean: multiply every element of the thing (scalar, vector, matrix, nd-array) by the scalar. That's why the other occurrences of * in the expression for q are ok - they all involve multiplying two scalars or multiplying 1 scalar and 1 vector. (Any or all of them could be .* instead and that would be ok too.) Only the .* between the exp() and the cos() expressions involves multiplying two vectors.]
Matt Thomas
Matt Thomas le 13 Mai 2022
thank you much for the help. Ok, when you say scalar, is that jus a single number?
im trying to see the difference of scalars. In math, i know what it means, but in matlab or programing i do not.
if i did a=[1 2 3 4]
is this a scalar or a matrix of just 1 row?
how do you let a be a list of numbers that are just scalars? IE
a=(list of numbers such as 1,2,3,4)
c=3
b= a*c so it would return 1*3,2*3,3*3....etc
thanks
"when you say scalar, is that jus a single number?"
Yes.
"if i did a=[1 2 3 4] is this a scalar or a matrix of just 1 row?"
a = [1 2 3 4] is a vector (not a scalar), specifically a row vector. (You can consider it a matrix with one row as well.) A vector is like a 1-dimensional matrix, it can be a row vector:
a = [1 2 3 4]
a = 1×4
1 2 3 4
or a column vector:
a = [1; 2; 3; 4]
a = 4×1
1 2 3 4
"how do you let a be a list of numbers that are just scalars? IE a=(list of numbers such as 1,2,3,4)"
It's exactly the same: a = [1 2 3 4]
(There is no term "list" in MATLAB lingo. In fact, whatever you might think of as a "list" would properly be called a "vector" in MATLAB, for the most part.)
a = [1 2 3 4];
c = 3;
b = a*c
b = 1×4
3 6 9 12
Matt Thomas
Matt Thomas le 13 Mai 2022
awesome. thanks. Vectors to me is something like velocity with speed and direction haha. I have never programed before so this is all new. Im in my senior year of medical and aerospace engineering and the class i am taking is a freshman class because i HATE programming i have put it off this long haha.
However, i am devoted to learning matlab.
so a scalar is just a single varible defined as a single number such as a=3?
thanks again for all the help. I will most likely be an active member on here.
"so a scalar is just a single varible defined as a single number such as a=3?"
That's right, since we've only been talking about numbers. But technically you can have scalar (and vector, and matrix, and higher-dimensional arrays) other things besides numbers, e.g., a scalar character:
c = 'a'
c = 'a'
or a scalar string:
s = "Alice"
s = "Alice"
or a scalar cell array:
C = {'Bob'}
C = 1×1 cell array
{'Bob'}
or a scalar struct:
S = struct('name','Bob','age',24)
S = struct with fields:
name: 'Bob' age: 24
etc. Or you can have vectors of those things:
c = 'abc'
c = 'abc'
s = ["Alice" "Bob" "Charlie"]
s = 1×3 string array
"Alice" "Bob" "Charlie"
C = {'Alice' 'Bob' 'Charlie'}
C = 1×3 cell array
{'Alice'} {'Bob'} {'Charlie'}
S = struct('name',{'Alice' 'Bob' 'Charlie'},'age',{22 24 34})
S = 1×3 struct array with fields:
name age
Or 2-dimensional (or higher) arrays of those things (or any other type of thing) too.
m = magic(5) % matrix of numbers
m = 5×5
17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9
c = num2str(m) % 2d character array (not usually called a "matrix of characters" but it really is)
c = 5×18 char array
'17 24 1 8 15' '23 5 7 14 16' ' 4 6 13 20 22' '10 12 19 21 3' '11 18 25 2 9'
C = arrayfun(@num2str,m,'UniformOutput',false) % 2d cell array (again, not usually called a "cell matrix" but it is)
C = 5×5 cell array
{'17'} {'24'} {'1' } {'8' } {'15'} {'23'} {'5' } {'7' } {'14'} {'16'} {'4' } {'6' } {'13'} {'20'} {'22'} {'10'} {'12'} {'19'} {'21'} {'3' } {'11'} {'18'} {'25'} {'2' } {'9' }
S = struct('value',C) % 2d struct array
S = 5×5 struct array with fields:
value
So the terms "scalar", "vector", "matrix", "array" don't imply anything about the class of the variable, only the dimensionality.
  • a scalar is 1-by-1
  • a vector is n-by-1 (column vector) or 1-by-n (row vector), for some n
  • a matrix is m-by-n, for some m and n (a lot of times people use the term "matrix" to imply a matrix of numbers, but it doesn't have to be)
  • an array is a generic term that can refer to an object of any dimensionality
"thanks again for all the help. I will most likely be an active member on here."
You're welcome! (for the help), and welcome to MATLAB Answers. By the way, if someone here helps you out or solves your problem or answers your question, it's courteous to Vote for their answer and/or click "Accept This Answer". (And in the case of accepting an answer it helps future visitors who have the same problem/question know that the given solution works.)
Matt Thomas
Matt Thomas le 14 Mai 2022
wow, thank you for all of this information. for sure helps me understand better
Matt J
Matt J le 12 Août 2023
If so, you should Accept-click the answer, and likewise with other valid answers to your posted questions.
John D'Errico
John D'Errico le 12 Août 2023
Now over a year old, and unaccepted. I did so myself.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by