ENGR-25_Programming-1.ppt 3 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Prob Solve 1st Step → PLOT it Advice for Every Engineer and Applied Mathematician or Physicist: RRule-1: When in Doubt PLOT IT! RRule-2: If you don’t KNOW when to DOUBT, then PLOT EVERYTHING
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
The 2The 2 ndnd O
rder Model
Order M
odel% Bruce Mayer, PE * 04Nov06% ENGR25 % file = Prob5_47_Chem_Concentration_0611.m% Find the Order of the chemical reaction%% CLEAR out any previous runsclear%% The Data Vectorst = [0,50,100,200,300];C = [0.01,0.0079,0.0065,0.0048,0.0038];%% WHEN IN DOUBT => PLOT%% in this plot vs t to reveal Rcn Order: ln(C) & 1/C%%% the Xformed DataVectors for RCN ORDERCfirst = log(C);Csecond = 1./C;%% Check which one gives straight linesubplot(2,1,1)plot(t,Cfirst,t,Cfirst,'*'), xlabel('t'), ylabel('1st Ord => ln(C)'), gridsubplot(2,1,2)plot(t,Csecond,t,Csecond,'o'), xlabel('t'), ylabel('2nd Ord =>1/C'), grid%% After Comparing two curves, 2nd order gives much straighter line%% use PolyFit to fit to 1/C(t)= k*t + 1/C0 => Y = mX + B%% Xform to Line => 1/C => Y, t => X, k => m, 1/C0 => B% Calc k & C0 showing in scientific notationformat short ek_1overC0 = polyfit(t,Csecond,1)k = k_1overC0(1)C0 = 1/k_1overC0(2)
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
The MeshG
rid PlotThe M
eshGrid Plot
% Bruce Mayer, PE * 04Nov06% ENGR25 % file = Prob5_57_Point_Charges_meshgrid_Plot_0611.m% Surface Plot eField from two Point Charges%% CLEAR out any previous runsclear% The Constant Parametersq1 = 2e-10; q2 = 4e-10; % in Coulombsepsilon = 8.854e-12; % in Farad/m%% Note the distances, r1 & r2, to any point(x,y) in the field by pythagorus% * r1 = sqrt((x-0.3)^2 + y^2)% * r2 = sqrt((x+0.3)^2 + y^2)%% Construct a 25x25 mesh[X Y] = meshgrid(-0.25:0.010:0.25);%% find r1 & r2 by pythagorus and array-opsr1 = sqrt((X-0.3).^2 +Y.^2); % note dots used with array operationr2 = sqrt((X-(-0.3)).^2 +Y.^2); % note dots used with array operation% use vectors r1 & r2, and array ops to find VV = (1/(4*pi*epsilon))*(q1./r1 + q2./r2);%% use %-Comment to toggle between SURF & MESHC plots% surf(X,Y,V), xlabel('X-distance'), ylabel('Y-distance'),... zlabel('Elect. Potential (V)'), title('2 Point-Charges Electical Field'),... grid onmeshc(X,Y,V), , xlabel('X-distance'), ylabel('Y-distance'),... zlabel('Elect. Potential (V)'), title('2 Point-Charges Electical Field'),... grid on
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
The Loop PlotThe Loop Plot
% Bruce Mayer, PE * 04Nov06 * ENGR25 % file = Prob5_57_Point_Charges_Loop_Plot_0611.m% Surface Plot eField from two Point ChargesClear % CLEAR out any previous runs% The Constant Parametersq1 = 2e-10; q2 = 4e-10; % in Coulombsepsilon = 8.854e-12; % in Farad/m% Note the distances, r1 & r2, to any point(x,y) in the field by pythagorus% * r1 = sqrt((x-0.3)^2 + y^2)% * r2 = sqrt((x+0.3)^2 + y^2)% Build up From Square XY Plane%% r1 goes to q1 at (0.3,0)%% r2 goes to q2 at (-0.3,0)x = linspace(-.25, .25, 50); %50 pts over x-range y = linspace(-.25, .25, 50); %50 pts over y-range for k = 1:length(x) for m = 1:length(y) % calc r1 & r2 using pythagorus r1 = sqrt((x(k)-0.3)^2 + y(m)^2); r2 = sqrt((x(k)-(-0.3))^2 + y(m)^2); % Find V based on r1 and r1 V(k,m) = (1/(4*pi*epsilon))*(q1/r1 +q2/r2); % Note that V is a 2D array using the x & y indices endendX = x;Y = y;% use %-Comment to toggle between SURF & MESHC plotssurf(X,Y,V), xlabel('X-distance'), ylabel('Y-distance'),... zlabel('Elect. Potential (V)'), title('2 Point-Charges Electical Field'),... grid on%meshc(X,Y,V), , xlabel('X-distance'), ylabel('Y-distance'),... zlabel('Elect. Potential (V)'), title('2 Point-Charges Electical Field'),... grid on