Chapter3.1-2 - 1/12/09 Script files. In the process of...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1/12/09 Script files. In the process of zooming on the solution we have repeatedly used certain lines. We might as well put them into a file using the Matlab editor. So I save the following lines y=cos(x) plot(x,y) grid xlabel('x') ylabel('cos(x)') In a file I call cosroot. Then I use it >> x=0:0.1:2; >> cosroot y = Columns 1 through 10 1.0000 0.9950 0.9801 0.9553 0.9211 0.8776 0.8253 0.7648 0.6967 0.6216 Columns 11 through 20 0.5403 0.4536 0.3624 0.2675 0.1700 0.0707 0.0292 0.1288 0.2272 0.3233 Column 21 0.4161 1 0.5 cos(x) 0 -0.5 0 0.2 0.4 0.6 0.8 1 x 1.2 1.4 1.6 1.8 2 And zoom >> x=linspace(1.4,1.6,10); cosroot y = 0.1700 0.1480 0.1260 0.1039 0.0818 0.0596 0.0375 0.0152 0.0070 0.0292 0.2 0.15 0.1 cos(x) 0.05 0 -0.05 1.35 1.4 1.45 x 1.5 1.55 1.6 >> It is also possible to make this a function, so that we can include the linspace in the file. function y=cosinter(xl,xu,n) %generates x values in the interval(xl,xu) and plots the cosine function i %the interval x=linspace(xl,xu,n); y=cos(x) plot(x,y) grid xlabel('x') ylabel('cos(x)') Then we invoke the function >> y=cosinter(1.5,1.6,10); y = 0.0707 0.0596 0.0486 0.0375 0.0263 0.0152 0.0041 0.0070 0.0181 0.0292 0.08 0.06 0.04 cos(x) 0.02 0 -0.02 -0.04 1.5 1.52 1.54 1.56 x 1.58 1.6 1.62 1.64 We can also have the function prompt us for the data, using the input command. Now the Mfile will be function y=cosprompt %generates x values in the interval(xl,xu) and plots the cosine function i %the interval xl=input('xlower') xu=input('xupper') n=input('number of intervals') x=linspace(xl,xu,n); y=cos(x) plot(x,y) grid xlabel('x') ylabel('cos(x)') And the output is: >> cosprompt xlower 1.5 xl = 1.5000 xupper 1.6 xu = 1.6000 number of intervals 11 n = 11 y = Columns 1 through 10 0.0707 0.0608 0.0508 0.0408 0.0308 0.0208 0.0108 0.0008 0.0092 0.0192 Column 11 0.0292 ans = Columns 1 through 10 0.0707 0.0608 0.0508 0.0408 0.0308 0.0208 0.0108 0.0008 0.0092 0.0192 Column 11 0.0292 0.08 0.06 0.04 cos(x) 0.02 0 -0.02 -0.04 1.5 1.52 1.54 1.56 x 1.58 1.6 1.62 1.64 To display information, we have the disp command. Consider calculating the eigenvalues of a matrix. Generate first a simple matrix sig=ones(3,3) sig = 1 1 1 1 1 1 1 1 1 >> disp('eigenvalues') eigenvalues >> disp(eig(sig)) 0.0000 0 3.0000 Notice that even though two of the eigenvalues are exactly zero, for the first one, computation errors apparently give a small negative value. format short e >> disp(eig(sig)) 3.3307e016 0 3.0000e+000 Instead of changing the display format every time we want to check something like that, we can also specify the format by using an fprintf command instead of a display command. >> fprintf('eigenvalues \n, %e %e %e', principal) eigenvalues , 3.330669e016 0.000000e+000 3.000000e+000>> Assuming that the matrix is a stress matrix, we can create an Mfile to calculate the maximum shear function maxshear(stress_tensor) %generates the maximum shear stress for a given stress tensor principal=eig(stress_tensor); maxshear=(max(principal)min(principal))/2; fprintf('maximum shear= %e', maxshear); Then we invoke it as >> maxshear(sig) maximum shear= 1.500000e+000>> ...
View Full Document

This note was uploaded on 03/27/2012 for the course EGM 3344 taught by Professor Raphaelhaftka during the Spring '09 term at University of Florida.

Ask a homework question - tutors are online