Docsity

Homework 2 solution Problem 2.27 | ME 2004, Study notes of Mechanical Engineering

Toggle sidebar
bg1
Problem 2.27
Objective
Plot erf(alpha*x) for 0 < x < 5 with alpha = 0.1 to 1.1 by increments of 0.2.
Matlab Code 1
% Pr. 2.27, "Numerical Methods with MatLab", Gerald Recktenwald
% Objective: Plot erf(alpha*x)
% for 0 < x < 5 with alpha=0.1 to 1.1 by 0.2.
% --- generate data
x = linspace(0,5);
alpha = 0.1 : 0.2 : 1.1;
% --- plot data
figure
for n = 1:length(alpha)
alphax = alpha(n)*x;
y = erf(alphax);
hold on
plot(x,y)
end
xlabel('x')
ylabel('erf(alpha*x)')
legend( num2str(alpha') )
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
x
erf(alpha*x)
0.1
0.3
0.5
0.7
0.9
1.1
pf2

Partial preview of the text

Download Homework 2 solution Problem 2.27 | ME 2004 and more Study notes Mechanical Engineering in PDF only on Docsity!

Problem 2.

Objective Plot erf(alphax) for 0 < x < 5 with alpha = 0.1 to 1.1 by increments of 0.2. Matlab Code 1 % Pr. 2.27, "Numerical Methods with MatLab", Gerald Recktenwald % Objective: Plot erf(alphax) % for 0 < x < 5 with alpha=0.1 to 1.1 by 0.2. % --- generate data x = linspace(0,5); alpha = 0.1 : 0.2 : 1.1; % --- plot data figure for n = 1:length(alpha) alphax = alpha(n)x; y = erf(alphax); hold on plot(x,y) end xlabel('x') ylabel('erf(alphax)') legend( num2str(alpha') ) 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 0

1 x erf(alpha*x)

Matlab Code 2 % "Numerical Methods with MatLab", Gerald Recktenwald % Problem 2. % Objective: % Plot erf(alphax) for 0 < x < 5 with alpha = 0.1 to 1.1 with increments of 0.2. % --- generate data x = linspace(0,5); alpha = 0.1 : 0.2 : 1.1; alphax = x'alpha; y = erf(alphax); % --- plot data plot(x,y) xlabel('x') ylabel('erf(alpha*x)') legend( num2str(alpha') ) 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 0

1 x erf(alpha*x)

Comments Method 1 perhaps seems more natural. Method 2 makes better use of MATLAB’s vectorized capabilities and is the more efficient, preferred way to solve this problem