Commit 4d5dfc9a authored by Paul McCarthy's avatar Paul McCarthy 🚵
Browse files

Merge branch 'saad-edits' into 'master'

Saad edits

See merge request fsl/pytreat-practicals-2020!21
parents e471dc28 b9a8c9bb
This diff is collapsed.
%% Play with model fitting
% experimental parameters
TEs = [10 40 50 60 80];
TRs = [.8 1 1.5 2];
[TEs,TRs] = meshgrid(TEs,TRs);
TEs = TEs(:)'; TRs = TRs(:)';
% forward model
forward = @(p)( p(1)*exp(-p(3)*TEs).*(1-exp(-p(2)*TRs)));
% simulate data
true_p = [100,1/.8,1/50];
data = forward(true_p);
snr = 50;
noise_std = 100/snr;
noise = randn(size(data))*noise_std;
data = data+noise;
plot(data)
%%
% cost function is mean squared error (MSE)
cf = @(x)( mean( (forward(x)-data).^2 ) );
% initial guess
p0 = [200,1/1,1/70];
% using fminsearch (Nelder-Mead)
p = fminsearch(@(x) cf(x),p0);
% plot result
figure,hold on
plot(data,'.')
plot(forward(p))
%% The below uses fminunc, which allows morre flexibility
% (like choosing the algorithm or providing gradients and Hessians)
options = optimoptions('fminunc','Display','off','Algorithm','quasi-newton');
[x,fval] = fminunc(cf,p0,options);
figure,hold on
plot(data,'.')
plot(forward(x))
This diff is collapsed.
This diff is collapsed.
%% Fitting Radial Basis Functions to some data
% Generate a noisy sine wave and plot it
x = linspace(0,10,100);
y = sin(3*x) + randn(size(x))*.5;
figure,
plot(x,y)
%% Fit RBF
% This defines a RBF atom function
sig = 2;
rbf = @(x,c)(exp(-(x-c).^2/sig^2));
% design matrix for fitting
xi = linspace(0,10,20);
desmat = zeros(length(x),length(xi));
for i=1:length(xi)
% each column is an RBF centered around xi
desmat(:,i) = rbf(x,xi(i));
end
% fit model
beta = desmat\y';
% plot fit
figure(1),hold on
plot(x,y,'.','markersize',10)
h = plot(x,desmat,'k'); for i =1:20;h(i).Color=[0,0,0,0.2];end
plot(x,desmat*beta,'-','linewidth',2)
% make it a little prettier
grid on
xlabel('x')
ylabel('y')
set(gca,'fontsize',16)
title('RBF fitting')
%% save figure
print -depsc ~/Desktop/RBF.eps
close(1)
!open ~/Desktop/RBF.eps
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment