i am getting error in line 18 of invalid use of operator.please help me solving this
2 views (last 30 days)
Show older comments
deepak mishra
on 15 Apr 2021
Commented: deepak mishra
on 17 Apr 2021
clear all
close all
m1 =12000;
m2 =10000;
m3 =8000;
k1 =3000;
k2 =2400;
k3 =1800;
A =[( k1 + k2 )/ m1 - k2 / m1 0 ; - k2 / m2 ( k2 + k3 )/ m2 - k3 / m2 ; 0 - k3 / m3 k3 / m3 ];
[V , D ]= eig ( A );
omega = diag ( D ).^0.5
modes = V ;
modes (: ,1)= modes (: ,1)/ modes (3 ,1);
modes (: ,2)= modes (: ,2)/ modes (3 ,2);
modes (: ,3)= modes (: ,3)/ modes (3 ,3);
modes
figure (1);
subplot (1 ,3 ,1);
plot ([0; modes (:,1)] ,0:3 ,’bo-’ );
ylabel ( ’ Floor ’ );
xlabel ( ’ Relative Displacement ’ );
mytitle = sprintf ( ’ \\ omega =%.2 f rad / s ’ , omega (1));
title ( mytitle )
subplot (1 ,3 ,2);
plot ([0; modes (: ,2)] ,0:3 , ’bo - ’ );
xlabel ( ’ Relative Displacement ’ );
mytitle = sprintf ( ’ \\ omega =%.2 f rad / s ’ , omega (2));
title ( mytitle )
subplot (1 ,3 ,3);
plot ([0; modes (: ,3)] ,0:3 , ’bo - ’ );
xlabel ( ’ Relative Displacement ’ );
mytitle = sprintf ( ’ \\ omega =%.2 f rad / s ’ , omega (3));
title ( mytitle )
2 Comments
Bjorn Gustavsson
on 15 Apr 2021
When running your code it gives an error at:
[V , D ]= eig ( A );
Because A has become a 3 x 2 matrix. Fix that - and give us the error-message.
Accepted Answer
deepak mishra
on 15 Apr 2021
2 Comments
Bjorn Gustavsson
on 15 Apr 2021
Now you have accepted this comment/question of yours as the ANSWER to your problem. That will hide it from other people. First you'll have to "unaccept" this answer. Then start to look at each successive line of your code and inspect the results they produce. For example after assigning the matrix A using your code I get:
A =
0.2500 0
-0.2400 0.2400
-0.2250 0.2250
Is that consistent with your expectations?
More Answers (2)
Bjorn Gustavsson
on 15 Apr 2021
Edited: Bjorn Gustavsson
on 15 Apr 2021
OK, so you want to solve 3 coupled equations of motion for some harmonically varying solution. First you have to understand the physics, then the mathematics and then you can solve this numerically.
1, physics.
Eq of motion:
Harmonic solutions ->
For a number of coupled oscillators you get
Where A has to be n-by-n, where in your case n is 3.
Here you should be able to identify your first problem, since A becomes 3-by-2!
You should be able to figure out what goes wrong in your assignment of A, it has to do with the interpretation of which terms are added(or subtracted) together to form a matrix-element. To not have that happen you should insert , (commas) between to separate the matrix-elements on a row. This is a very treacherous error we all(?) encountered when starting to use matlab.
HTH
0 Comments
Jan
on 15 Apr 2021
Edited: Jan
on 15 Apr 2021
For me line 18 is:
subplot (1 ,3 ,1);
I do not see a problem here. But in the next line:
plot ([0; modes (:,1)] ,0:3 ,’bo-’ );
% ^ ^
Use single quotes ' instead. Did you copy the code from a DOCX, PDF or from the net? Then the quotes are converted sometimes. You should see the problem in the syntax highlighting already.
Please care for posting a copy of the complete error message and do not let the reader guess or count, which line is failing.
Notes:
- Omit the brute clearing header "clear all; close all". This is not useful, but teachers suggest it only, because their teachers had suggested it 20 years ago. This is "cargo cult programming" and a widespread programming anti-pattern.
- sqrt(X) is faster than the power operation X.^0.5.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!