Matlab Example Tensile Strength for Carbon Nanotube Program

 

Matlab Program:

 

T_100= 100*10^9;

T_200 =200*10^9; % TENSILE STRENGHT FOR CARBON NANOTUBE.

D = (0.25:0.25:25); % DIAMETER IN METERS.

F_100 = T_100.*((pi.*D.^2)./4);

F_200 = T_200.*((pi.*D.^2)./4);

figure(1);

plot(D, F_100, D, F_200), title(‘Diameter Vs. Force (Carbon Nanotube)’);

xlabel(‘Diameter, m’);

ylabel(‘Force, N’);

legend(‘TS 100 GPa’, ‘TS 200 GPa’);

 

 

 

PROBLEMS WITH TWO VARIABLES Matlab

All of the calculations we have done thus far have used only one variable. Of course, most physical phenomena can vary with many different factors. In this section, we consider how to perform the same calculations when the variables are represented by vectors.

Consider the following MATLAB ®

statements:

x = 3;

y = 5;

A = x * y

Since x and y are scalars, it’s an easy calculation: x· y= 15, or

A =

15

Now, let’s see what happens if x is a matrix and y is still a scalar:

x = 1:5;

returns five values of x . Because y is still a scalar with only one value (5),

A = x * y

returns

A =

5 10 15 20 25

This is still a review. But what happens if y is now a vector? Then

y = 1:3;

A = x * y

returns an error statement:

??? Error using = => *

Inner matrix dimensions must agree.

This error statement reminds us that the asterisk is the operator for matrix mul-tiplication, which is not what we want. We want the dot-asterisk operator ( .* ), which will perform an element-by-element multiplication. However, the two vectors, x and y , will need to be the same length for this to work. Thus,

y = linspace(1,3,5)

creates a new vector y with five evenly spaced elements:

y =

1.0000 1.5000 2.0000 2.5000 3.0000

A = x .* y

A =

1 3 6 10 15

SUBPLOTS Matlab

The subplot command allows you to subdivide the graphing window into a grid of m rows and n columns.

The function:

subplot(m,n,p)

splits the figure into an mn matrix. The variable p identifi es the portion of the window where the next plot will be drawn. For example, if the command

subplot(2,2,1)

is used, the window is divided into two rows and two columns, and the plot is drawn in the upper left-hand window ( Figure 5.14 ).

The windows are numbered from left to right, top to bottom. Similarly, the follow-ing commands split the graph window into a top plot and a bottom plot:

x = 0:pi/20:2*pi;

subplot(2,1,1)

plot(x,sin(x))

subplot(2,1,2)

plot(x,sin(2*x)

The first graph is drawn in the top window, since p1. Then the subplot com-mand is used again to draw the next graph in the bottom window. Figure 5.15 shows both graphs.

Titles are added above each subwindow as the graphs are drawn, as are x – and y -axis labels and any annotation desired.

Plots with More than One Line Matlab

Plots with More than One Line

A plot with more than one line can be created in several ways. By default, the execution of a second plot tatement will erase the first plot. However, you can layer plots on top of one another by using the hold on command. Execute the following statements to create a plot with both functions plotted on the same graph, as shown in Figure 5.4 :

x = 0:pi/100:2*pi;

y1 = cos(x*4);

plot(x,y1)

y2 = sin(x);

hold on;

plot(x, y2)

Semicolons are optional on both the plot statement and the hold on state-ment. MATLAB ® will continue to layer the plots until the hold off command is executed:

hold off

Another way to create a graph with multiple lines is to request both lines in a single plot command. MATLAB ®interprets the input to plot as alternating x and y vec-tors, as in

plot(X1, Y1, X2, Y2)

where the variables X1 , Y1 form an ordered set of values to be plotted and X2 , Y2 form a second ordered set of values. Using the data from the previous example,

plot(x, y1, x, y2)

produces the same graph as Figure 5.4 , with one exception: The two lines are differ-ent colors. MATLAB ® uses a default plotting color (blue) for the first line drawn in a plot command. In the hold on approach, each line is drawn in a separate plot command and thus is the same color. By requesting two lines in a single command, such as plot(x,y1,x,y2) , the second line defaults to green, allowing the user to

distinguish between the two plots.

If the plot function is called with a single matrix argument, MATLAB ®draws a separate line for each column of the matrix. The x -axis is labeled with the row index vector, 1: k , where k is the number of rows in the matrix. This produces an evenly spaced plot, sometimes called a line plot. If plot is called with two argu-ments, one a vector and the other a matrix, MATLAB ®successively plots a line for each row in the matrix. For example, we can combine y1 and y2 into a single matrix and plot against x :

Y = [y1; y2];

plot(x,Y)

This creates the same plot as Figure 5.4 , with each line a different color. Here’s another more complicated example:

X = 0:pi/100:2*pi;

Y1 = cos(X)*2;

Y2 = cos(X)*3;

Y3 = cos(X)*4;

Y4 = cos(X)*5;

Z = [Y1; Y2; Y3; Y4];

plot(X, Y1, X, Y2, X, Y3, X, Y4)

This code produces the same result ( Figure 5.5 ) as

plot(X, Z)

A function of two variables, the peaks function produces sample data that are useful for demonstrating certain graphing functions. (The data are created by scal-ing and translating Gaussian distributions.) Calling peaks with a single argument n

will create an nn matrix. We can use peaks to demonstrate the power of using a matrix argument in the plot function. The command

plot(peaks(100))

results in the impressive graph in Figure 5.6 . The input to the plot function created by peaks is a 100X100 matrix. Notice that the x -axis goes from 1 to 100, the index numbers of the data. You undoubtedly can’t tell, but there are 100 lines drawn to create this graph—one for each column.