Vector giving weird multiplication
2 views (last 30 days)
Show older comments
prompt = {'Mass 1','Mass 2','K1','K2','K3'};
dlg_title = 'Constants';
num_lines = 1;
defaultans = {'1','1','1','1','1'};
temp = inputdlg(prompt,dlg_title,num_lines,defaultans);
[m1,m2,k1,k2,k3] = temp{:};
A=[-(k1+k2)/(m1),k2/m1;k2/m2,-(k2+k3)/m2];
[v,d]=eig(A);
v1 = v(1:2,1);
v2 = v(1:2,2);
prompt = {'Starting x1','Starting x2'};
dlg_title = 'Start x relative to equalib.';
num_lines = 1;
defaultans = {'1','0'};
answer = inputdlg(prompt,dlg_title,num_lines,defaultans);
[x1,x2] = answer{:};
x = [x1;x2];
gamma=inv(v)*x;
So Im very new to Matlab and have been "tweaking" another code I found online that relates two masses attached to 3 springs and they have only a linear motion. My problem is in the output of gamma, where Im supposed to be getting an answer of x = [.7071;.7071], but Im getting x = [.7071,68.5894] which is really off. Also when I was trying to figure out where something went wrong, I noticed that if I set "e" as a 2x2 identity matrix and go e*x, I get the weird answer of [49;48]. Thedefault answers are what I used. Any help would be greatly appreciated
7 Comments
John D'Errico
on 29 Apr 2016
There are lots of reasons why I would avoid this code like the plague. Yes, you found this code online. NOT everything you find online is true, good, or safe for you. Certainly, not every piece of code you find is well written code. This is a case where the code was written by someone who actually knew little about numerical methods or good coding practices in MATLAB.
Several things catch my eye immediately. First you mention the variable gamma. Oops! Bad idea there, to define a variable named gamma. Why? Because gamma is the name of a useful function. One day, if you are using this script, you will find that you then wanted to use the function gamma. That will cause a conflict, and you will then ask a question online like why does my code fail?
Next is the use of inv. Inv (det is another bad thing to use) is one of those tools that really should almost never be used. There are better choices almost always. But students learn the idea of matrix inverses, and think they are fine to use, again, like det. But there are a lot of things you find in books that are not always the best way to write code.
For example, if you have a problem like A*x=b. In a book, you'll probably see the solution written as essentially
x = A^-1 * b
or as
x = inv(A)*b
or there may be some complex formula where an inverse was used, simply because that is the simple way to write the formula. Most of the time though, you will be better off if you use alternatives like
x = A\b
when you actually write the code in MATLAB.
Finally, another clue about seeing code written by a relative novice is when you see code laden with inputs as a way to bring in information. LEARN TO USE FUNCTIONS INSTEAD! Your capabilities in MATLAB will grow by leaps and bounds once you start to learn what a function is and how to use them effectively.
Accepted Answer
Stephen23
on 29 Apr 2016
Edited: Stephen23
on 29 Apr 2016
inputdlg outputs a cell array of strings, which you then join into a character array using [x1,x2] = answer{:};. So the answers you are getting, e.g.
>> eye(2)*x
ans =
49
48
are completely correct: multiply the character code by the identity matrix, and this returns the character codes for '1' and '0' (49 and 48 respectively).
I suggest that you change the last two lines of code to this:
>> x = [str2double(x1);str2double(x2)];
>> gam = v\x;
gam =
0.70711
0.70711
Addendum
Note that it is actually very easy to tell the difference between a character digit and a numeric value, based on their column justification (char are left aligned):
>> '1' % character
ans =
1
>> 1 % numeric
ans =
1
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!