calling function from separate document for fitting

Hi all, I am working on this curve fitting project and I am trying out different methods to optimise the run time. One of the methods I am trying involes having 2 seperate scripts, with one script containing the function used for fitting and curve fitting (call this script 1), and the other script containing codes to load the data and process the data (call this script 2). However, when I try to call script 1 from 2, I get a ton of error messages. Could I please ask why is this the problem? Thank you! My codes are attached below:
tic
%% Preparation
clc; clear
format short
TAdata = readmatrix("FCPIB-293K-2.5mW-400nm-Jan072021 -ibg -bg -chirp.csv"); % insert file path within parenthesis
MBdata = readmatrix("carrier temp.xlsx");
P = load("Steady_State_Parameter_Values.mat");
Function = 'Transient_Absorption';
%% Preamble
% Fundamental constants
h = 4.1356677*10^-15; % units: eV/ Hz
c = 3*10^8; % SI units
kB = 8.617333268*10^-5; % units: eV/ K
% Data
Wavelength = TAdata(:, 1);% units: nm
E = (h*c)./(Wavelength*10^-9);
delay_t = TAdata(1, :);
delay_T = MBdata(:, 2);
carrier_T = MBdata(:, 3);
% Data for fitting
min_E = 1.5;
max_E = 2.0;
Range_E = E >= min_E & E <= max_E;
Range_W = Wavelength >= (h*c)/(max_E*10^-9) & Wavelength <= (h*c)/(min_E*10^-9);
E_p = E(Range_E); % selected probe energies
for n = 1:length(carrier_T)
a(1, n) = find(delay_T(n) == delay_t);
data_new2 = TAdata(Range_W, a);
end
%% Data for fitting and calling fitting function & solver
for i = 1:length(delay_T)
p = P.p;
A1 = p(1,1);
A2 = p(1,2);
Eg = p(1,3);
Eb = p(1,4);
R = p(1,5);
g = p(1,6);
deltaAbs = data_new2(:, i);
lb = [0, 0, 0, 0.3, 0, 0]; ub = [20, 0.05, 0.05, 2, 2, 1];
x0 = [1.6244, 0.0346, 0.03, 1.5139, 1, 0.3];
carrierT = carrier_T(i);
[x] = TA_fitting_function(A1, A2, Eg, Eb, R, g, x0,x,carrierT,E_p,deltaAbs,lb,ub,i,LegendText, Function);
end
Unrecognized function or variable 'x'.
%% Table of parameter values
time_delay = ['0.5 ps'; '1.0 ps'; '2.0 ps'; '4.0 ps'];
Eg_prime = x(:, 1);
Eb_prime = x(:, 2);
g_prime = x(:, 3);
Ef_q = x(:, 4);
f1 = x(:, 5);
f2 = x(:, 6);
T = table(time_delay, Eg_prime, Eb_prime, g_prime, Ef_q, f1, f2);
disp(T)
toc

 採用された回答

Torsten
Torsten 2024 年 7 月 19 日
移動済み: Torsten 2024 年 7 月 19 日

0 投票

However, when I try to call script 1 from 2, I get a ton of error messages. Could I please ask why is this the problem?
You can't call scripts, you can only call functions.
If you define EM_TA_wR_CC and EM_TA_wR_EC as function handles depending on x and E_p (as you use them in the plot commands), you don't need to pass x to "TA_fitting_function", and the error will disappear.

5 件のコメント

Jack
Jack 2024 年 7 月 21 日
Hi, SFLR. I see. But what if I were to not use function handles? And I think I might have made my code confusing. I am actually not passing x into TA_fitting_function, but instead using TA_fitting_function to solve for x.
Torsten
Torsten 2024 年 7 月 21 日
What exactly is your question ?
Jack
Jack 2024 年 7 月 21 日
I should've been more specific. In the script named "TA_fitting_function", it contains the function that I am using for my curve fitting as well as the curve fitting options (I'm using lsqcurvefit) as well as plot commands. What I am trying to do now is to input my data on one of the scripts, and then use the stuff in "TA_fitting_function". But right now I can't seem to make that work...
Torsten
Torsten 2024 年 7 月 21 日
編集済み: Torsten 2024 年 7 月 21 日
In the script named "TA_fitting_function"
At the moment, TA_fitting_function is not a script, but a function. And as far as I know, it's not possible to run several scripts simultaneously in MATLAB.
What I am trying to do now is to input my data on one of the scripts, and then use the stuff in "TA_fitting_function".
Define the things you want to use in "TA_fitting_function" and that you think should be set before in your script and call "TA_fitting_function" with these inputs. You can even define the name of the function you want to use for fitting as a function name as
[sol] = TA_fitting_function(@fun,other inputs)
Jack
Jack 2024 年 7 月 21 日
I see. Will definitely give it a try. Thank you!

サインインしてコメントする。

その他の回答 (1 件)

Divyajyoti Nayak
Divyajyoti Nayak 2024 年 7 月 19 日
編集済み: Divyajyoti Nayak 2024 年 7 月 19 日

0 投票

Hi @Jack, the error that you are recieving is because you are passing 'x' as an argument of 'TA_fitting_function' but not defining it anywhere in the script.
Hope that helps!

カテゴリ

ヘルプ センター および File ExchangeGet Started with Curve Fitting Toolbox についてさらに検索

製品

リリース

R2024a

タグ

質問済み:

2024 年 7 月 19 日

コメント済み:

2024 年 7 月 21 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by