User Tools

Site Tools


gibson:teaching:spring-2018:math445:lecture:publish

Formatting labs with Matlab publish

You can use the Matlab publish function to generate a nicely-formatted PDF of your lab work from a Matlab script. Here's an illustration that repeats material from today's lecture on estimating log-linear relationships from plots, as if it were work I wanted to turn in for lab 3.

Write a matlab script in file lab3.m. Mark the title and problems with %%. Note that this script loads a couple data files; these are provided below.

%% John Gibson, Lab 3 Math 445
 
%% Problem 1: 
% Fit function to x,y data in file data1.asc
 
d = load('data1.asc'); % load data from file data1.asc into matrix d
x = d(:,1);            % assign 1st column of d to vector x
y = d(:,2);            % assign 2nd column of d to vector y
 
fig = figure(1); 
fig.Position = [0 0 450 300];           % fig size 450x300 pixels
clf();                                  % clear figure 
semilogy(x,y, 'bo-', 'linewidth', 1.5); % plot data as blue line
hold on;                                % hold figure
grid on                                 % plot a coordinate grid
 
yapprox = 400*10.^(-1.1*x); % constants estimated from graph of data
 
plot(x, yapprox, 'r-', 'linewidth',1.5) % solid red line
xlabel('x')
ylabel('y')
legend('data', 'approximation')
title('Problem 1: exponential fit, y(x) = c 10^{mx}');
 
 
%% Problem 2: 
% Fit function to x,y data in file data3.asc
 
d = load('data3.asc'); % load data from file data1.asc into matrix d
x = d(:,1);            % assign 1st column of d to vector x
y = d(:,2);            % assign 2nd column of d to vector y
clf();                 % clear figure
 
loglog(x,y, 'bo-', 'linewidth', 1.5);  % plot data as blue line
hold on;                               % hold figure
grid on                                % plot grid
 
yapprox = 0.2*x.^(-0.58); % consts estimated from graph of data
 
plot(x, yapprox, 'r-', 'linewidth',1.5) % solid red line
xlabel('x')
ylabel('y')
legend('data', 'approximation')
title('Problem 2: power-law fit y(x) = c x^m');

Run the script through the Matlab publish function at the command prompt

>> publish('lab3.m', 'pdf');

This produces a file lab3.pdf in an html subdirectory

>> ls html
lab3.pdf

You can submit your lab work by uploading this file to Canvas. The finished product looks like this (first page only).

Data files

data1.asc
% x             y
1.06e+00 2.22e+01
2.71e+00 3.38e-01
5.53e+00 2.72e-04
7.87e+00 7.39e-07
9.12e+00 3.19e-08
1.24e+01 8.06e-12
1.40e+01 1.41e-13
1.59e+01 1.05e-15
1.80e+01 5.32e-18
2.04e+01 1.13e-20
data3.asc
4.04e-04 2.02e+01
1.67e-03 8.85e+00
2.04e-03 7.88e+00
8.38e-03 3.46e+00
1.73e-02 2.27e+00
2.51e-02 1.82e+00
5.33e-02 1.18e+00
1.06e-01 7.90e-01
2.09e-01 5.32e-01
1.69e+00 1.57e-01
1.80e+00 1.52e-01
1.92e+00 1.46e-01
2.52e+00 1.25e-01
5.89e+00 7.63e-02
6.97e+00 6.92e-02
1.28e+02 1.27e-02
1.35e+02 1.23e-02
3.11e+02 7.58e-03
5.59e+02 5.39e-03
6.54e+03 1.29e-03
gibson/teaching/spring-2018/math445/lecture/publish.txt · Last modified: 2018/02/06 10:22 by gibson