Error message - matrix dimensions must agree

1 Ansicht (letzte 30 Tage)
Barry McCartin
Barry McCartin am 13 Mär. 2014
Kommentiert: Chris C am 13 Mär. 2014
Hi I am inputting the following line of code to solve a laplace irreducible quadratic equation. Its quite long and I have checked the brackets numerous times and I think they are correct. I think the problem is arising due to the use of the multiplication sign and I am unsure where I need to use the .* and the *. Could someone please advise? Thank you.
i=(c/b)*((1-(exp((-a/2)*t))).*cos(sqrt(b-((a^2)/4))*t)+(a/2)*(1/(sqrt(b-(a^2)/4).*t))*sin(sqrt(b-(a^2)/4)*t));
I then get the following error message
Error using / Matrix dimensions must agree.
Error in laplace (line 104) i=(A)*((1-(exp((-a/2)*t))).*cos(sqrt(b-((a^2)/4))*t)+(a/2)*(1/(sqrt(b-(a^2)/4).*t))*sin(sqrt(b-(a^2)/4)*t));

Antworten (2)

Chris C
Chris C am 13 Mär. 2014
The .* is used whenever you have to arrays or matrices that you want to multiply each individual element with the corresponding element in the other array or matrix. * is used when you want good old fashioned matrix muliplication. For me to be more clear I need to know what the variables in your code stand for (i.e. arrays, constants, matrices).
  1 Kommentar
Barry McCartin
Barry McCartin am 13 Mär. 2014
Thanks a million for your response. The complete code is below & the irreducible is at the end of the program. I hope there's enough here. Thanks again for your response
clc; % clear display for user
%display information for program on the command screen disp('This program will display the result and plot the graphs of') disp('linear, repeated linear and irreducible quadratic factors') disp('It will also state if the system is stable or unstable') disp('The formula (d^2*i)/(dt^2) + a*(di/dt)+bi(t)=c given i(0)=0 and i`(0)=0 is used') disp('The user is prompted to enter the values for a, b & c')
a=input('\n\nEnter a value for a: ');%prompt user for a value b=input('\n\nEnter a value for b: ');%prompt user for b value c=input('\n\nEnter a value for c: ');%prompt user for c value fprintf('\n');
d=a^2; %let d equal a^2 e=4*b; %let e equal 4*b
if(d>e) %if a^2 is greater than 4*b it is a linear factor
fprintf('This is a simple linear factor \n') %prints this message if it is a linear factor
r1=(-a+sqrt(d-e))/(2); %calculations for r1
r2=(-a-sqrt(d-e))/(2); %calculations for r2
fprintf('r1 = %f and r2 = %f\n', r1,r2) %prints the values of r1 & r2
%linear factor calculations for A, B & C
A=(c/(r1*r2));
B=(c/(r1*(r1-r2)));
C=(c/(r2*(r2-r1)));
%let time t be between 0 and 5 in steps of 0.1
t=0:0.1:10;
%formula for i(t) for a simple linear factor
i= A + B*exp(r1*t) + C*exp(r2*t);
%plot the graph of the simple linear factor
plot(t,i,'--r', 'lineWidth',2);
title('Simple Linear Factor', 'FontSize',20, 'fontWeight','bold', 'color','red')
xlabel('Time - t', 'FontSize', 15, 'fontWeight', 'bold')%label for x axis
ylabel('Amplitude', 'FontSize', 15, 'fontWeight', 'bold')%label for y axis
grid on;
fprintf('\nThe answer is i(t)=%.2f+%.2fe^%.2ft+%.2fe^%.2ft \n\n',A,B,r1,C,r2)
if(r1<0&&r2<0)%stable system if the powers of e are negative
fprintf('The System is Stable')
else
fprintf('The System is Unstable')
end
elseif d==e %if a^2 is equal to 4*b it is a repeated linear factor fprintf('This is a repeated linear factor\n')
%A, B & C Calculations for a repeated linear factor
%when a^2 equals 4*b the value of r1 is -a/2
A = (c/(r1^2));
B = -A;
C = c/(-r1);
fprintf('r1 = %f ',r1)
%formula for i(t) for repeated linear factor
i= A + B*exp(r1*t) - C*(t.*exp(r1*t));
plot(t,i,'--g', 'lineWidth',2); %plot the graph
title('Repeated Linear Factor','FontSize',20, 'fontWeight','bold', 'color','green')
xlabel('Time - t', 'FontSize', 15, 'fontWeight', 'bold')%label for x axis
ylabel('Amplitude', 'FontSize', 15, 'fontWeight', 'bold')%label for y axis
grid on;
fprintf('\nThe answer is i(t)=%.2f+%.2fe^%.2ft+%.2fe^%.2ft \n\n',A,B,r1,C,r2)
if(r1<0)%it is stable system if the power of e is negative
fprintf('This is a stable system')
else
fprintf('This is an unstable system')
end
elseif(d<e) %if a^2 is less than 4*b it is an irreducible quadractic factor fprintf('This is an irreducible quadratic factor\n')
%A, B & C calculations for an irreducible quadratic factor
A=(c/b);
B=(-A);
C=(((-c*a)/b));
%formula for i(t) for irreducible quadratic factor
i=(A)*((1-(exp((-a/2)*t))).*(cos(sqrt(b-((a^2)/4)).*t)+(a/2)*(1/(sqrt(b-(a^2)/4).*t))*(sin(sqrt(b-(a^2)/4).*t))));
i2=abs(i);%i2 is the absolute value of i
if(a>0)%if a is greater than zero it is a negative number and the power of e remains negative
fprintf('This is a stable system\n')
else
fprintf('This system is not stable\n')
end
l = (sqrt(b-a^2/4));
m = (1/sqrt(b-a^2/4));
fprintf('i(t) = %.2f(1+e^%.2ft)*cos(%.2ft)+%.2f*1/%.2f*sin%.2f \n\n',(c/b),(-a/2),l,(a/2),m,l)
plot(t,i2,'--b', 'lineWidth',2);
title('Irreducible Quadratic Factor', 'FontSize',20, 'fontWeight','bold', 'Color','blue')
xlabel('Time - t', 'FontSize', 15, 'fontWeight', 'bold')%label for x axis
ylabel('Amplitude', 'FontSize', 15, 'fontWeight', 'bold')%label for y axis
grid on;
end

Melden Sie sich an, um zu kommentieren.


Chris C
Chris C am 13 Mär. 2014
Okay, I found them (I think). In line 104 that you reference above it looks like there are several variables referenced: A,a,t and b. A, a and b are all contants and t is your only array. Therefore every instance of t multiplied or divided by itself needs a . before the operation.
I found one instance that hasn't been done correctly i.e...
(1/(sqrt(b-(a^2)/4).*t))*(sin(sqrt(b-(a^2)/4).*t))))
There needs to be a . between these two terms and it should look like this...
(1/(sqrt(b-(a^2)/4).*t)).*(sin(sqrt(b-(a^2)/4).*t))))
Hope that helps.
  2 Kommentare
Barry McCartin
Barry McCartin am 13 Mär. 2014
Thank you very much indeed, I will give that a go. Thanks for taking the time to look at it. I really appreciate it.
Chris C
Chris C am 13 Mär. 2014
No problem. Please don't forget to "Accept" an answer that satisfactorily answers your question. :)

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by