Final Exam Review
MATLAB
Create a function:
function [y1, y2] = name(x1, x2, x3)
Anonymous Functions:
y = @(x,z) 3*x + 6*z
Open files
% opens as write only
fileID = fopen(filename, 'w');
% write to file
fprintf(fileID, '%.6f %.6f\n', x(i), y(i));
% close file
fclose(fileID);
Random
rand(1)
Generates a random decimal number between 0 and 1.
Graphing
2D Plotting
If x and y are a vectors of values:
plot(x,y)
Using linspace and an equation:
x = linspace(1,10,100);
y = (2./x).^3;
plot(x,y,'go-') % green, circle marker, solid line
3D Plotting
plot3(x,y,z)
Plotting area
xlim(lower_bound, upper_bound);
Labling
Use hold on to add stuff to the same figure.
% add a title
title('title');
% label axis
xlabel('x');
ylabel('y');
zlabel('z');
% add text
text(x,y,'text');
Euler's Method:
Given
- first order ordinary differential equation: as
dxdt - initial condition
We determine for by using Euler's Method. for
for i = 1:max_steps
y(i+1) = y(i) + dydt(t(i), y(i))*(h);
end
Runge-Kutta Method
These methods acheive the accurracy of the taylor series approach without requiring the calculations of higher derivatives.
<-- Increment Function
's are constants
<-- 's and 's are constants ...
The order is chosen as and the values of a, p, q are evalulated by setting the general equation equal to terms in a Taylor series expansion.
Fourth Order Runge Kutta (RK4):
You need the following input
dydtthe name of the M-file that evalues that ODEtspanconsisting oftiandtfthat corresponds to the initial and final times.y0is the initial values of the dependent variablehis the stetpsize
Before we start:
- check that
nargin >= 4(if you are accepting optional inputs) - check that
tspanis span some time - populate the independent vector (create time
t)
The computation:
for i=1:(length(x)-1) % calculation loop
k1 = dydt(x(i),y(i));
k2 = dydt(x(i)+0.5*h,y(i)+0.5*h*k1);
k3 = dydt((x(i)+0.5*h),(y(i)+0.5*h*k2));
k4 = dydt((x(i)+h),(y(i)+k3*h));
y(i+1) = y(i) + (1/6)*(k1+2*k2+2*k3+k4)*h; % main equation
end
Solving system of equations
If A is the matrix and B is the column of solutions. Solve with one of the following ways
% inverse
x = inv(A)*b
% rref
r = rref([a b]);
x = r(:, 4);
Solve an equation
syms x
eqn = sin(x) == x^2 - 1;
S = solve(eqn,x)
Polynomials
Fit a set of points to a polynomial with n degrees with polyfit:
p = polyfit(x,y,1)
p contains two coefficients that describe a linear curve fit.
Use polyval to evaluate the a polynomial at the given points
p = polyfit(x,y,1);
x = [25 30 35 40 45];
y = polyfit(p, x);
Mean Squared Error (MSE)
Matlab function: err = immse(x,y).
Doing it manually:
- where n is sample size
Newton's Method
Approximates the real zeros of a function by using tangent lines to approximate the graph of the functions near its x-intercepts.
Consider function f that is continuous and differentiable on the interval [a b].
Choose a starting point x1.
The equation of the tangent line is
If we let and solve for we find the new estimate:
We can improve on the value by calculating another estimate using the old estimate
Generally:
The code look something like
function [xr, iter, err, results] = newtons_method(f, fder, xi, iter_max, tol)
xr = xi;
for iter=1:iter_max
xrold = xr;
xr = newtons_update(f, fder, xrold);
err(iter) = abs((xrold - xr)/xrold);
results(iter) = xr;
if (err(iter) < tol)
break;
end
end
end
where newtons_update is:
function [xr] = newtons_update(f,fder, xi)
xr = xi - f(xi)/fder(xi);
end
Least Squares Approximation
Do it with matlab's built in function: polyfit.
It's goal is to minimize error betweent the fit line and the data poins.
- Error can also be written as
Taking the derivative of this to minimize involves quite a bit of work... refer to lab10
Reading Input
Use fopen to open the file.
Use fscapf(fileID, '%f') to read numbers
- or use
%cto read as strings
You can choose a whole line with tline = fgetl(fileID) and iterate through the whole file until it reaches the end. Here is an example of reading and displaying each line of a text file:
fid = fopen('badpoem.txt');
while ~feof(fid)
tline = fgetl(fid);
disp(tline)
end
fclose(fid);Linked from Computational Modeling