How do I apply an if statement to every row of a matrix of varying number of rows?
6 views (last 30 days)
Show older comments
I'm trying to write within an if statement for the program load a matrix from a text file. The first column of the matrix are m values, the second are c values, and the third are k values. The program needs to see if m, c, and k are greater than zero. If they're not, it outputs an error message, an if they are, then it uses those numbers to do some other calculations, and then it needs to see if each available value of R, a number calculated by the program, is less than, equal to, or greater than 1.
I've got it working if I specifically type into the program which row of R to look at, but I don't know how to make it automatically run through each row of R.
Please help!
Here's the code I have when the row numbers are included in the program:
if true
A=load('Dynamics.txt');
m=A(:,1);
c=A(:,2);
k=A(:,3);
p=c./m;
q=k./m;
Wn=q.^(1/2);
R=p./(2.*Wn);
Wd=Wn.*(1-(p.^2)).^0.5;
if (m(1,1)>0)&&(c(1,1)>0)&&(k(1,1)>0)
if R(1,1)>1
fprintf('1) Natural frequency= %1.3g rad/s; Damping ratio=%1.3g; System is overdamped, damped frequency does not exist.\n', Wn(1,1), R(1,1))
elseif R(1,1)==1
fprinf('1) Natural frequency= %1.3g rad/s; Damping ratio=%1.3g; System is critically damped, damped frequency does not exist.\n', Wn(1,1), R(1,1))
elseif R(1,1)<1
fprintf('1) Natural frequency=%1.3g rad/s; Damping ratio=%1.3g; Damped frequency=%1.3g rad/s; System is underdamped.\n', Wn(1,1), R(1,1), Wd(1,1))
end
else disp '1) Error in input values.'
end
end
Any help I can get would be super appreciated!!
0 Comments
Answers (1)
sixwwwwww
on 30 Oct 2013
Dear Shannon, you can put the if-else statements in a loop as follows:
A=load('Dynamics.txt');
m=A(:,1);
c=A(:,2);
k=A(:,3);
p=c./m;
q=k./m;
Wn=q.^(1/2);
R=p./(2.*Wn);
Wd=Wn.*(1-(p.^2)).^0.5;
for x = 1:numel(R)
if (m(x)>0)&&(c(x)>0)&&(k(x)>0)
if R(x)>1
fprintf('1) Natural frequency= %1.3g rad/s; Damping ratio=%1.3g; System is overdamped, damped frequency does not exist.\n', Wn(x), R(x))
elseif R(x)==1
fprinf('1) Natural frequency= %1.3g rad/s; Damping ratio=%1.3g; System is critically damped, damped frequency does not exist.\n', Wn(x), R(x))
elseif R(x)<1
fprintf('1) Natural frequency=%1.3g rad/s; Damping ratio=%1.3g; Damped frequency=%1.3g rad/s; System is underdamped.\n', Wn(x), R(x), Wd(x))
end
else
disp '1) Error in input values.'
end
end
I hope it is what you need. Good luck!
2 Comments
See Also
Categories
Find more on Encryption / Cryptography in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!