12.04.2015 Views

Solving Problems in Dynamics and Vibrations Using MATLAB ...

Solving Problems in Dynamics and Vibrations Using MATLAB ...

Solving Problems in Dynamics and Vibrations Using MATLAB ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

6<br />

Solve Comm<strong>and</strong><br />

The ‘solve’ comm<strong>and</strong> is a predef<strong>in</strong>ed function <strong>in</strong> <strong>MATLAB</strong>. The code for solv<strong>in</strong>g the above<br />

equations us<strong>in</strong>g the ‘solve’ comm<strong>and</strong> is as shown. Open a new M-File <strong>and</strong> type the follow<strong>in</strong>g<br />

code.<br />

% To solve the l<strong>in</strong>ear equations us<strong>in</strong>g the solve comm<strong>and</strong><br />

p = ‘x + 2*y = 6’;<br />

q = ‘x – y = 0’;<br />

[x,y] = solve(p,q)<br />

Subs Comm<strong>and</strong><br />

This comm<strong>and</strong> is expla<strong>in</strong>ed by means of the follow<strong>in</strong>g example. Suppose you want to solve the<br />

follow<strong>in</strong>g l<strong>in</strong>ear equations:<br />

x + 2y<br />

= a + 6<br />

x − y = a<br />

Note that there are three unknown <strong>and</strong> only two equations. So we are required to solve for ‘x’<br />

<strong>and</strong> ‘y’ <strong>in</strong> terms of ‘a’. To calculate the numerical values of ‘x’ <strong>and</strong> ‘y’ for different values of ‘a’,<br />

we use the subs comm<strong>and</strong>.<br />

The follow<strong>in</strong>g <strong>MATLAB</strong> code is used to demonstrate the ‘subs’ comm<strong>and</strong>.<br />

% To solve the l<strong>in</strong>ear equations us<strong>in</strong>g the subs comm<strong>and</strong><br />

p = ‘x + 2*y = a + 6’<br />

q = ‘x – y = a’<br />

[x,y] = solve(p,q)<br />

a = 0;<br />

[x] = subs(x)<br />

[y] = subs(y)<br />

Here the ‘solve’ comm<strong>and</strong> solves for the values of ‘x’ <strong>and</strong> ‘y’ <strong>in</strong> terms of ‘a’. Then ‘a’ is<br />

assigned the value of ‘0’. Then we substitute the assigned value of ‘a’ <strong>in</strong> ‘x’ <strong>and</strong> ‘y’ to get the<br />

numerical value.<br />

Suppose a varies from 1 to 10 <strong>and</strong> we need to f<strong>in</strong>d the value of ‘x’ <strong>and</strong> ‘y’ for each value of ‘a’<br />

then use the follow<strong>in</strong>g code:<br />

% To solve the l<strong>in</strong>ear equations us<strong>in</strong>g the subs comm<strong>and</strong>

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!