Why am I not able to plot this?
1 view (last 30 days)
Show older comments
Hello everyone,
can anyone help me plot this, please?
I get an error at the first line.
Thank you very much
function output = baymbt_predict(mbt5me,prior_mean,prior_std,Tmodel,Type)
% Ensure column vector.
mbt5me=mbt5me(:);
% load appropriate model
if strcmp(Type,"lake")
load('baymbt0_params_lake.mat','b_draws_final','tau2_draws_final');
elseif strcmp(Type,"soil")
if strcmp(Tmodel,"T")
load('baymbt_params_soil.mat','b_draws_final','tau2_draws_final');
elseif strcmp(Tmodel,"T0")
load('baymbt0_params_soil.mat','b_draws_final','tau2_draws_final');
else
error('TModel not recognized - choices are "T" and "T0"');
end
else
error('Type not recognized - choices are "soil" and "lake"');
end
% get dimensions of time series and draws
nd = length(mbt5me);
n_draws = length(tau2_draws_final);
% parse parameters
alpha = b_draws_final(:,2);
betaT = b_draws_final(:,1);
sigma = sqrt(tau2_draws_final);
% Prior mean and inverse covariance matrix
pmu = repmat(ones(nd, 1) * prior_mean,1,n_draws);
pinv_cov = repmat(prior_std,nd,n_draws).^-2;
% Posterior calculations
post_mean_num = pinv_cov .* pmu + repmat(sigma',nd,1).^-2 .* repmat(betaT',nd,1) .* (mbt5me - repmat(alpha',nd,1));
post_mean_den = pinv_cov + repmat(betaT',nd,1).^2 .* repmat(sigma',nd,1).^-2;
post_mean = post_mean_num ./ post_mean_den;
post_sig = sqrt(post_mean_den.^-1);
output.ens = post_mean + randn(nd,n_draws).*post_sig;
output.prior_mean = prior_mean;
output.prior_std = prior_std;
if strcmp(Tmodel,"T0")
% if using BayMBT0, truncate at T < 0
output.ens(output.ens < 0) = NaN;
else
end
output.T = prctile(sort(output.ens,2),[2.5 50 97.5],2);
2 Comments
John D'Errico
on 9 Mar 2022
When you get an error, show the error. THE COMPLETE ERROR. EVERYTHING IN RED. Show how you tried to call the function in question, and which variables were used to pass in as arguments.
In fact, here, the odds are good you were trying to use the run command, or something similar, since you say you get an error at the first line.
Answers (1)
DGM
on 9 Mar 2022
Three things:
- How are you calling the function? This file contains code prior to the function header, so it's not a function file. It's a script with a local function. If you want to use it as a script, it needs to be called within the script, prior to the function header. If you want to use it externally as a function file, remove everything prior to the function header.
- Close the function with an end statement.
- The given .mat files don't have the same filename as what the code is looking for.
See Also
Categories
Find more on Whos in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!