MATLAB Inner matrix dimensions must agree - matlab

I have t=linspace(1, 10, 91)
I have to define with those values the function y=(((e^(t/10))sin(t))/((t^2)+1)
I write this in MATLAB:
y=((exp(t/10)*sin(t))/((t.^2)+1)
Matlab says:
??? Error using ==> mtimes
Inner matrix dimensions must agree.
I then tried to fix it whatever way possible and put a period before * and this is what I got:
y=((exp(t/10).*sin(t))/((t.^2)+1))
y =
0.0077
I think this isn't the answer because it is not giving me the answer for each value of the matrix. I really don't know what happened.
Can someone help?

Your missing the dot before /:
y=((exp(t/10).*sin(t)) ./ ((t.^2)+1))
Note: You can easily find problems like this on your own. You could have done
((exp(t/10).*sin(t))
and seen that it works as expected. Then you could try ((t.^2)+1)). Wow, that works as well. Thus, the problem has to be cause by the /. From there to ./ it is just a small step.

Related

Defining A Differential Equation Using matlabFunction

I am trying solve the differential equation by first putting it in normal, which, if I did it correctly, should be dx/dt = a8 1/3 x - 1/3 b8, where a8 is the second derivative, and b8 is the third derivative. Here is a portion of my code:
matlabFunction( [a8 +x8/3 - b8/3; a8; b8],'vars',{t,[b8;a8;x8]},'file','DE_11')
And here are the errors I get:
Error using sym/cat>checkDimensions (line 75)
CAT arguments dimensions are not consistent.
Error in sym/cat>catMany (line 38)
[resz, ranges] = checkDimensions(sz,dim);
Error in sym/cat (line 27)
ySym = catMany(dim, strs);
Error in sym/vertcat (line 19)
ySym = cat(1,args{:});
I honestly do not know what these messages are hinting at. I was hoping someone could help me decipher these error messages, and show me where I went wrong. My intention is, after these issues have been resolved, to use ode45 to solve the differential equation.
Thank you, and I apologize for my ignorance.
EDIT: Okay, after having aimlessly tried various things, I was able to get it to "work." Here is what I changed it to:
matlabFunction([b8;a8;a8 - b8/3 - x8/3], 'vars',{t8,x8,[b8,a8]},'file','DE_11')
However, I am not really certain as to why that worked, or if its even the correct input. Could someone perhaps show me why it worked? I understand that this [b8;a8;a8 - b8/3 - x8/3] represents a column vector containing my unknown functions, but I do not exactly understand this part {t8,x8,[b8,a8]}. I know that we are defining variables, but why do we use curly brackets, and why are some enclosed in square brackets, and others are not?
the error is caused by
[a8 +x8/3 - b8/3; a8; b8]
as this attempts to create a matrix with inconsistent dimensions as the first space separates column entries in the first row. using no spaces or spaces either side of the operators will solve this...
[a8 + x8/3 - b8/3; a8; b8] or [a8+x8/3-b8/3; a8; b8]
should work as intended.
see http://www.mathworks.co.uk/help/matlab/matlab_prog/symbol-reference.html#bsgigzp-52

What does this Pdesurf error message mean?

I run
pdesurf(mesh.p, mesh.t, u)
I got
Error using pdesurf (line 25)
Illegal solution format.
PDESURF expects input of the form pdesurf(p,t,u). u must either be a column vector and the same length as p, or a row vector and the same length as t. I don't know how big your mesh.p and mesh.t variables are, so I can't say for sure, but it could be because you need to transpose your vector. It is possible that the error might be corrected by changing your code to
pdesurf(mesh.p, mesh.t, ufun(0:0.01:1,0:0.01:1)') % Note the transpose
If this doesn't work, then you need to make sure that either
size(t,2)==size(u,2)
or
size(p,2)==size(u,1)
Transpose u
pdesurf(mesh.p, mesh.t, u')

I am not able to figure out how to carry out the Matrices multiplication in matlab

I have to carry out the following operation
R=[0,0.5,-0.25;-0.25,0,0.25;0,0,0.25];
B=[0,k21,k31;k12,0,k32;0,0,k];
G=inv(R).*B;
g=det(G);
but Matlab is showing the following error
??? Error using ==> horzcat
CAT arguments dimensions are not consistent.
Error in ==> g at 60
B=[0,k21,k31;k12,0,k32;0,0,k];
K21,K31,K12,K32 and k all have dimensions of 923334 by 1. Can anyone help me how can I carry out the following operation.
Your code works well for me. Check that the k-values (k12,k31,k32...) are scalars (or 1x1 dimension)
EDIT :
For the case you mention, k's are nx1, one simple way is to perform a loop:
R=[0,0.5,-0.25;-0.25,0,0.25;0,0,0.25];
for ii=1:length(k)
B=[0,k21(ii),k31(ii);k12(ii),0,k32(ii);0,0,k(ii)];
G=inv(R).*B;
g(ii)=det(G);
end
There is also a "vectorized" way to do that, but it seems to be good enough...

MATLAB: not calculating correctly...user error?

I have been looking at this code for a while now, and cannot figure out why matlab is not calculating correctly. Does anyone see anything that I may be doing wrong with this code?
((1-EU_P2par3(:,1))*US_P2par3(:,1))+((1-EU_P2par3(:,2))*US_P2par3(:,2))+((1-EU_P2par3(:,3))*US_P2par3(:,3))+((1-EU_P2par3(:,4))*US_P2par3(:,4))+((1-EU_P2par3(:,5))*US_P2par3(:,5))+((1-EU_P2par3(:,6)*US_P2par3(:,6)))+((1-EU_P2par3(:,7))*US_P2par3(:,7))
Thanks for all the help!
In cases like this, good code formatting is your friend. Using an ellipsis (i.e. ..., the line continuation symbol) to create a multi-line statement can help greatly...
It looks like you have a parenthesis in the wrong place. Your code looks like this:
result = ((1-EU_P2par3(:,1))*US_P2par3(:,1))+...
((1-EU_P2par3(:,2))*US_P2par3(:,2))+...
((1-EU_P2par3(:,3))*US_P2par3(:,3))+...
((1-EU_P2par3(:,4))*US_P2par3(:,4))+...
((1-EU_P2par3(:,5))*US_P2par3(:,5))+...
((1-EU_P2par3(:,6)*US_P2par3(:,6)))+... %# Notice something here?
((1-EU_P2par3(:,7))*US_P2par3(:,7));
And you probably want this:
result = ((1-EU_P2par3(:,1))*US_P2par3(:,1))+...
((1-EU_P2par3(:,2))*US_P2par3(:,2))+...
((1-EU_P2par3(:,3))*US_P2par3(:,3))+...
((1-EU_P2par3(:,4))*US_P2par3(:,4))+...
((1-EU_P2par3(:,5))*US_P2par3(:,5))+...
((1-EU_P2par3(:,6))*US_P2par3(:,6))+... %# Notice the change?
((1-EU_P2par3(:,7))*US_P2par3(:,7));
EDIT:
In addition, as Darren mentions in his answer, you will likely have to use the element-wise multiplication operator .* instead of the matrix multiplication operator *. Explanations of the arithmetic operators can be found here.
Also, your calculation can be greatly simplified by vectorizing it using the function SUM, like so:
result = sum((1-EU_P2par3(:,1:7)).*US_P2par3(:,1:7),2);
Try the following example.
xy = rand(10,2);
a = xy(:,1)*xy(:,2);
% ??? Error using ==> mtimes
% Inner matrix dimensions must agree.
a = xy(:,1).*xy(:,2);
The error arises when you attempt to multiply vectors together. You must use the .* operator to get element-wise multiplication
Hope that helps

matlab error using m power

so if I run this function in matlab
sim1(row,1:512)= ((image(row,1:512,1)-a(1,1)));
it runs fine. now if I modify it to take the square like this
sim1(row,1:512)= ((image(row,1:512,1)-a(1,1)))^2;
it gives me the error, error using ==> mpower
matrix dimensions must agree. Why is this giving me the error, I can do this element by element but I have a lot of data and it will take forever.
It seems you want to do an element by element power which is .^2 not ^2
That is, change to
sim1(row,1:512)= ((image(row,1:512,1)-a(1,1))).^2;