Matlab dot notation - matlab

Doing some matlab work and keep getting: "Error using * Inner matrix dimensions must agree."
This is my code, what can I do to fix it?
a=2
b=5
x=[0:(pi/40):(pi/2)]
y=b*(exp(1).^(-a*x))*sin(b*x)*(0.012*(x.^4) -0.15*(x.^3) + 0.075*(x.^2) + 2.5*x)

You are mixing elemnt-wise (.*) and matrix (*) multiplication. Since you want element-wise multiplication for the vector x, you have to make sure that every vector/vector operation is element-wise. You just missed a few dots, it should be like this:
y=b*(exp(1).^(-a*x)).*sin(b*x).*(0.012*(x.^4)-0.15*(x.^3)+0.075*(x.^2)+2.5*x)

Related

Matlab function won't plot

Trying to plot a function on matlab, however the graph comes out completely empty
X = linspace(-2,2);
Y = (10*exp(X./10) - 7)/(exp(X.*(33/10)));
plot(X,Y);
You need to use element-wise operation for division:
Y = (10*exp(X./10) - 7)./(exp(X.*(33/10)));
Plotting will work fine then. The problem now is that Y is a one-element array.
I want to add here that, the only operation that should be element-wise operation is the division in the middle. The other operations don't need the '.' as the division of array by scalar as no other meaning. So, it should better be written like this:
Y = (10*exp(X/10) - 7)./(exp(X*(33/10)));
Just a few points to add on to #hesham_EE's excellent answer:
For beginners, it's best to only use * when you are doing matrix multiplication. For arithmetic and/or element-wise operations, stick with .*
It is helpful in debugging to print the output of each row of computation, i.e. omit the semicolon. This allows you to check your syntax. In this case, you would have noticed that Y was not what you had intended it to be.

Error using multiplication matlab

I am trying to plot this function in matlab but I get the error: Error using *
Inner matrix dimensions must agree
Why is this happening?
My code:
H_s=2;
f_zero=2;
f=0:0.001:0.01;
S_f=(5*(H_s).^2)/(16*f_zero)*(f/f_zero).^(-5)*exp(-(5/4)*f)
plot(f,S_f)
The first terms ((5*(H_s).^2)/(16*f_zero)*(f/f_zero).^(-5)) evaluate to a 1x11 matrix, as does the last term (exp(-(5/4)*f)). The matrix multiplication operator * indeed requires the inner dimensions to match.
But you were probably trying to do element-wise multiplication .*:
S_f=(5*(H_s).^2)/(16*f_zero)*(f/f_zero).^(-5).*exp(-(5/4)*f)

Plotting an equation with two input variables using meshgrid() in MATLAB - matrix incompatibility

I am trying to plot a 3D graph of an equation that has two input variables: time, t and spring constant, K in order to investigate the effect of K on the output. I have looked in to how to plot a function with two input variables using meshgrid() and converting the two inputs into compatible matrices.
For multiplying one of those inputs, say 't'. The multiplication sign needs to be preceded by '.' e.g y = t.*C (where C is some constant). For two inputs it is the same; e.g y = t.*C + K.^2.
However I cannot find how to do that for division, if the variable is in the numerator I assume you can simply write the expression as say: t*1/C. However how do you write it when the variable is in the denominator as in 'C/t'. I have tried placing '.' after 't' in the denominator however I get an error:
Error using /
Matrix dimensions must agree.
Also do I need to put the '.' after the variable in an addition?
Apologies if all this seems vague. I can put in the actual equation however it extremely long and it works when only t is a variable and K is a constant so the equation itself is sound.
The operations that have to be preceded with a . to apply element-wise are:
Multiplication: .*
Division: ./
Power: .^
Thus, if A, B, and C are arrays, you write
y = (A.*B./C).^2

Multiplying complex matrices

I'm trying to multiply two complex matrices (b+c*i), but I'm getting no results.
??? Error using ==> mtimes
Inner matrix dimensions must agree.
Error in ==> impedancaZ at 14
l=mtimes(R1,h)
I don't understand this error, as matrix dimension are the same (2 coluoms and 9 rows)
Can you help me?
Inner matrix dimensions must agree means that the inner dimensions of the matrices must match. If the first matrix has dimensions 2x9, then the second would need to be 9x(something). that's just basic liner algebra/matrix multiplication. In that case, you'll need to figure out what the second array should be. Maybe it's the transpose of what you expect it to be; instead of x*y, you might want x*y' (see the "prime" marker after the y?
Alternatively, maybe you want a "scalar multiply" rather than a "matrix multiply" for this. That is, you don't want to multiply the matrices x and y in the "linear algebra" sense, but you just want to multiply the elements of the arrays, element by element. In that case, you'd do x.*y (see the dot before the *?).
Unfortunately, I can't tell which is really correct for your situation without more context. You'll either have to supply some more information or figure it out yourself from the hints I've given.

I can't multiple numbers in matlab

I need to find rp for each x but I'm having a hard time because matlab gives me this error
Error in enee_408e_hw2_pb1 (line 6)
rp=(z2*cos(t2)-z1*cos(x))/(z2*cos(t2)+z1*cos(x))
here is my code
x=0:.01:pi/2;
n2=1.7;
t2=asin(sin(x)/n2);
z1=377*cos(x);
z2=377\cos(x);
rp=(z2*cos(t2)-z1*cos(x))/(z2*cos(t2)+z1*cos(x));
I want to calculate rp for each angle from 0 to pi/2 and then plot it. What am I doing wrong?
Every operation is perform on same cell in z1 and z2. Matlab/Ocatave calculate enquation based on variable's type. So if you put * between vectors it try to calculate cross product. Probably problem was \, I have no idea what Matlab tried to do with it. Adding . before operator change approach to more natural (for non mathematical folks) and multiply corresponding cells.
But still I'm not sure it this is what you expected
rp=(z2.*cos(t2)-z1.*cos(x))./(z2.*cos(t2)+z1.*cos(x));
First, why are you using \ in this line?
z2=377\cos(x);
There is a difference between \ and / in MATLAB.
It is time to learn about the difference between ./ and .* versus / and * in MATLAB.
When you want element-wise operations, use the operators with a dot.