Tag Archives: matlab
integerating the FPGA through the Matlab
the bitstream does not exist. please check the external console to make sure the bitstream generation os completed and try again?
this is the issue i am facing in the matlab.the bitstream does not exist. please check the external console to make sure the bitstream generation os completed and try again?
this is the issue i am facing in the matlab. the bitstream does not exist. please check the external console to make sure the bitstream generation os completed and try again?
this is the issue i am facing in the matlab. matlab, fpga MATLAB Answers — New Questions
SimScape MultiBody Solver Step Time – Simulation running longer
Hello,
I am currently working on a multibody model in simscape. I have the following issue:
My simulation time last too much. I set a stop time of 10sec but it lasts around 45sec.
I try to fix that through Configuration parameters -> Solver -> Solver selection fixed-step of 0.0002. But again it lasts 45sec and for bigger fixed-step size (0.002) then an error apears for degenerate mass distribution on different joints.
Could you please give me few ideas or directions to solve this?Hello,
I am currently working on a multibody model in simscape. I have the following issue:
My simulation time last too much. I set a stop time of 10sec but it lasts around 45sec.
I try to fix that through Configuration parameters -> Solver -> Solver selection fixed-step of 0.0002. But again it lasts 45sec and for bigger fixed-step size (0.002) then an error apears for degenerate mass distribution on different joints.
Could you please give me few ideas or directions to solve this? Hello,
I am currently working on a multibody model in simscape. I have the following issue:
My simulation time last too much. I set a stop time of 10sec but it lasts around 45sec.
I try to fix that through Configuration parameters -> Solver -> Solver selection fixed-step of 0.0002. But again it lasts 45sec and for bigger fixed-step size (0.002) then an error apears for degenerate mass distribution on different joints.
Could you please give me few ideas or directions to solve this? step-time, simscape MATLAB Answers — New Questions
Simulink IGBT block control issue
Hi all!
I have a simulink project for just trying out IGBT blocks, where at a later stage I will use them for IGBT inverter simulations.
At the moment, I am just doing some testing, where it just looks like (or appears like) I can’t turn off the IGBT transistors, no matter what gate ‘voltage’ I apply to their gates.
I have included an example (attached). When I ‘run’ my example simulink project, the voltage measurement scope will show zero volt – no matter what value of ‘voltage’ that I apply to the gate. In the project setup, I have arbitrary set the gate voltages to -500 (negative 500) volt.
I can get a 500 volt measurement to show on the scope only if I remove one of the IGBT blocks (or remove both IGBT blocks).
In my screenshot – I have added some comments to let everyone know the general details of the screenshot. I’m just attempting to find out the reason for the voltage measurement being zero volt, regardless of the applied gate voltage. I have found that the voltage measurement becomes 500V when one or both IGBT are removed.
Fig (above) – using project ibgt_switching.slx – has port 3 connected to GND, and 4 connected to -500 volt, with gate voltages connected to -500 volt. I am expecting the scope voltage measurement to be 500 volt. But the scope voltage measurement turns out to be zero volt.
Fig (above) – scope voltage measurement is showing zero volt.
The following screenshots (below) are for the case where I have removed one of the IGBT blocks from the vertical branch. The simulink project file for this is attached too. And it can be seen in the screenshots that we have 500 volt being measured by the scope.
Fig (above) – using project ibgt_switching_1.slx – showing one of the IGBT blocks removed. The simulation scope voltage gives the desired voltage of 500 volt.
Fig (above) – scope voltage measurement is showing 500 volt.
Also – related – but not directly to my main question. I notice that the gate of these IGBT blocks can be connected to a ‘constant’ block. But the gates of these IGBT blocks don’t seem to be able to be connected to signal lines. For example, when I try to connect the gate of the IGBT to the lines connected to port #3 or port #4 etc, I find that the line connection cannot be made (ie. a black coloured line does not form). Could someone let me know as well if it is actually possible to connect the gate of these IGBT blocks to the usual signal lines?
Thanks very much!Hi all!
I have a simulink project for just trying out IGBT blocks, where at a later stage I will use them for IGBT inverter simulations.
At the moment, I am just doing some testing, where it just looks like (or appears like) I can’t turn off the IGBT transistors, no matter what gate ‘voltage’ I apply to their gates.
I have included an example (attached). When I ‘run’ my example simulink project, the voltage measurement scope will show zero volt – no matter what value of ‘voltage’ that I apply to the gate. In the project setup, I have arbitrary set the gate voltages to -500 (negative 500) volt.
I can get a 500 volt measurement to show on the scope only if I remove one of the IGBT blocks (or remove both IGBT blocks).
In my screenshot – I have added some comments to let everyone know the general details of the screenshot. I’m just attempting to find out the reason for the voltage measurement being zero volt, regardless of the applied gate voltage. I have found that the voltage measurement becomes 500V when one or both IGBT are removed.
Fig (above) – using project ibgt_switching.slx – has port 3 connected to GND, and 4 connected to -500 volt, with gate voltages connected to -500 volt. I am expecting the scope voltage measurement to be 500 volt. But the scope voltage measurement turns out to be zero volt.
Fig (above) – scope voltage measurement is showing zero volt.
The following screenshots (below) are for the case where I have removed one of the IGBT blocks from the vertical branch. The simulink project file for this is attached too. And it can be seen in the screenshots that we have 500 volt being measured by the scope.
Fig (above) – using project ibgt_switching_1.slx – showing one of the IGBT blocks removed. The simulation scope voltage gives the desired voltage of 500 volt.
Fig (above) – scope voltage measurement is showing 500 volt.
Also – related – but not directly to my main question. I notice that the gate of these IGBT blocks can be connected to a ‘constant’ block. But the gates of these IGBT blocks don’t seem to be able to be connected to signal lines. For example, when I try to connect the gate of the IGBT to the lines connected to port #3 or port #4 etc, I find that the line connection cannot be made (ie. a black coloured line does not form). Could someone let me know as well if it is actually possible to connect the gate of these IGBT blocks to the usual signal lines?
Thanks very much! Hi all!
I have a simulink project for just trying out IGBT blocks, where at a later stage I will use them for IGBT inverter simulations.
At the moment, I am just doing some testing, where it just looks like (or appears like) I can’t turn off the IGBT transistors, no matter what gate ‘voltage’ I apply to their gates.
I have included an example (attached). When I ‘run’ my example simulink project, the voltage measurement scope will show zero volt – no matter what value of ‘voltage’ that I apply to the gate. In the project setup, I have arbitrary set the gate voltages to -500 (negative 500) volt.
I can get a 500 volt measurement to show on the scope only if I remove one of the IGBT blocks (or remove both IGBT blocks).
In my screenshot – I have added some comments to let everyone know the general details of the screenshot. I’m just attempting to find out the reason for the voltage measurement being zero volt, regardless of the applied gate voltage. I have found that the voltage measurement becomes 500V when one or both IGBT are removed.
Fig (above) – using project ibgt_switching.slx – has port 3 connected to GND, and 4 connected to -500 volt, with gate voltages connected to -500 volt. I am expecting the scope voltage measurement to be 500 volt. But the scope voltage measurement turns out to be zero volt.
Fig (above) – scope voltage measurement is showing zero volt.
The following screenshots (below) are for the case where I have removed one of the IGBT blocks from the vertical branch. The simulink project file for this is attached too. And it can be seen in the screenshots that we have 500 volt being measured by the scope.
Fig (above) – using project ibgt_switching_1.slx – showing one of the IGBT blocks removed. The simulation scope voltage gives the desired voltage of 500 volt.
Fig (above) – scope voltage measurement is showing 500 volt.
Also – related – but not directly to my main question. I notice that the gate of these IGBT blocks can be connected to a ‘constant’ block. But the gates of these IGBT blocks don’t seem to be able to be connected to signal lines. For example, when I try to connect the gate of the IGBT to the lines connected to port #3 or port #4 etc, I find that the line connection cannot be made (ie. a black coloured line does not form). Could someone let me know as well if it is actually possible to connect the gate of these IGBT blocks to the usual signal lines?
Thanks very much! igbt MATLAB Answers — New Questions
Remove harmonics background noise
Hi,
I have a signal sampled at 128 [kHz], it contains noisy spikes at harmonic multiples of 59.6 [Hz]. Tried different bandstop filtering techniques, but can’t seem to get a proper result.
Would appreciate it if something with some experience could elaborate how to remove the humming noise in the background.
Cheers
SammyHi,
I have a signal sampled at 128 [kHz], it contains noisy spikes at harmonic multiples of 59.6 [Hz]. Tried different bandstop filtering techniques, but can’t seem to get a proper result.
Would appreciate it if something with some experience could elaborate how to remove the humming noise in the background.
Cheers
Sammy Hi,
I have a signal sampled at 128 [kHz], it contains noisy spikes at harmonic multiples of 59.6 [Hz]. Tried different bandstop filtering techniques, but can’t seem to get a proper result.
Would appreciate it if something with some experience could elaborate how to remove the humming noise in the background.
Cheers
Sammy filter, noise, harmonics MATLAB Answers — New Questions
How do I install and set up MATLAB Web App Server?
How do I install and set up MATLAB Web App Server?How do I install and set up MATLAB Web App Server? How do I install and set up MATLAB Web App Server? matlab, web, app, server MATLAB Answers — New Questions
PSCP: What is this report metric?
This is the last metric shown in Chapter 2 of generated PSCP reports. It seems to be 100% unless there is a red check.In this case, there was 1 red check out of about 100 files processed. When the red check was eliminated, the value changed to 100%. I looked through the user guide and reference docs but did not find an answer.This is the last metric shown in Chapter 2 of generated PSCP reports. It seems to be 100% unless there is a red check.In this case, there was 1 red check out of about 100 files processed. When the red check was eliminated, the value changed to 100%. I looked through the user guide and reference docs but did not find an answer. This is the last metric shown in Chapter 2 of generated PSCP reports. It seems to be 100% unless there is a red check.In this case, there was 1 red check out of about 100 files processed. When the red check was eliminated, the value changed to 100%. I looked through the user guide and reference docs but did not find an answer. polyspace, reports MATLAB Answers — New Questions
Space in a text is taken as line break in UIcontrol
I have a long string variable like this.
tex = "Date=09-Jul-2024 Delta_E=-3.3317 Delta_U=300-320V Sigma=0.094335 MaxAbs=0.00056882 MeanAbs=-0.079426 MaxRel=0.012132 eanRel=-0.9771 AllowedDeviation=0.05-0.1"
I am trying to represent this in a fig as UIcontrol using this line of code
txt_title = uicontrol(‘Style’, ‘text’, ‘String’,tex,’FontSize’,10,’Position’, [100 200 180 150]);
But space in the string variable is read as line break. Why is that, i tried removing linebreak using strtrim, erase(tex,char(10)). But nothing is working.
I need this in one line and not like this. What is the error? Thank you.I have a long string variable like this.
tex = "Date=09-Jul-2024 Delta_E=-3.3317 Delta_U=300-320V Sigma=0.094335 MaxAbs=0.00056882 MeanAbs=-0.079426 MaxRel=0.012132 eanRel=-0.9771 AllowedDeviation=0.05-0.1"
I am trying to represent this in a fig as UIcontrol using this line of code
txt_title = uicontrol(‘Style’, ‘text’, ‘String’,tex,’FontSize’,10,’Position’, [100 200 180 150]);
But space in the string variable is read as line break. Why is that, i tried removing linebreak using strtrim, erase(tex,char(10)). But nothing is working.
I need this in one line and not like this. What is the error? Thank you. I have a long string variable like this.
tex = "Date=09-Jul-2024 Delta_E=-3.3317 Delta_U=300-320V Sigma=0.094335 MaxAbs=0.00056882 MeanAbs=-0.079426 MaxRel=0.012132 eanRel=-0.9771 AllowedDeviation=0.05-0.1"
I am trying to represent this in a fig as UIcontrol using this line of code
txt_title = uicontrol(‘Style’, ‘text’, ‘String’,tex,’FontSize’,10,’Position’, [100 200 180 150]);
But space in the string variable is read as line break. Why is that, i tried removing linebreak using strtrim, erase(tex,char(10)). But nothing is working.
I need this in one line and not like this. What is the error? Thank you. matlab MATLAB Answers — New Questions
Rate Transition in If-action Subsystems
Hello,
I am trying to setup a control system with different controller operating modes. I want to handle the selectionm of the operating mode with a if-action subsystem structure. I’m getting the following error that I am not completly sure how to handle:
‘ctrlActiveHandler_0_4_sim/Axis_Control/AbsPosMode/Horizontal_Axis_Control/Rate Transition’ is a rate transition block. A rate transition block is not allowed inside the if action subsystem ‘ctrlActiveHandler_0_4_sim/Axis_Control/AbsPosMode’ unless its input or output port sample time is asynchronous sample time. Consider placing the rate transition block outside the if action subsystem.
I can’t take the rate transition outside the subsystem as it ensures integrity between to different blocks. And I am not sure about the asynchrounus sample time. Can someone explain this error and its fix?
Thank you very much.Hello,
I am trying to setup a control system with different controller operating modes. I want to handle the selectionm of the operating mode with a if-action subsystem structure. I’m getting the following error that I am not completly sure how to handle:
‘ctrlActiveHandler_0_4_sim/Axis_Control/AbsPosMode/Horizontal_Axis_Control/Rate Transition’ is a rate transition block. A rate transition block is not allowed inside the if action subsystem ‘ctrlActiveHandler_0_4_sim/Axis_Control/AbsPosMode’ unless its input or output port sample time is asynchronous sample time. Consider placing the rate transition block outside the if action subsystem.
I can’t take the rate transition outside the subsystem as it ensures integrity between to different blocks. And I am not sure about the asynchrounus sample time. Can someone explain this error and its fix?
Thank you very much. Hello,
I am trying to setup a control system with different controller operating modes. I want to handle the selectionm of the operating mode with a if-action subsystem structure. I’m getting the following error that I am not completly sure how to handle:
‘ctrlActiveHandler_0_4_sim/Axis_Control/AbsPosMode/Horizontal_Axis_Control/Rate Transition’ is a rate transition block. A rate transition block is not allowed inside the if action subsystem ‘ctrlActiveHandler_0_4_sim/Axis_Control/AbsPosMode’ unless its input or output port sample time is asynchronous sample time. Consider placing the rate transition block outside the if action subsystem.
I can’t take the rate transition outside the subsystem as it ensures integrity between to different blocks. And I am not sure about the asynchrounus sample time. Can someone explain this error and its fix?
Thank you very much. action subsystem, rate transition, asynchronous sample time MATLAB Answers — New Questions
error using trainNetwork Invalid training data. Responses must be a matrix of numeric responses, or a N-by-1 cell array of sequences, where N is the number of sequences
Hi
I have this following code and the data set is traffic flow that gathered every 15 seconds and I want to predict data flow but i faced an error "Invalid training data. Responses must be a matrix of numeric responses, or a N-by-1 cell array of sequences, where N is the number of sequences. The feature dimension of all sequences must be the same"and I can’t fix it can anyone help me with it
My code is :
clc
close all
clear all
warning off
[~,~,flow_data] = xlsread(‘two_days.xlsx’); % Here we have two days data
data_mat = cell2mat(flow_data(2:end,3:4));
YTrain = data_mat(:,:)’;
XTrain = data_mat(:,:)’;
%
% %
XTrain = num2cell(XTrain,1)’;
YTrain = num2cell(YTrain,1)’;
numResponses = 1 ;
% numResponses = size(YTrain{1},1);
featureDimension = size(XTrain{1},1);
numHiddenUnits = 100;
layers = [ …
sequenceInputLayer(featureDimension)
lstmLayer(numHiddenUnits)
% dropoutLayer(0.1) %%0.5
fullyConnectedLayer(numResponses)
regressionLayer];
maxepochs = 500;
miniBatchSize = 1;
options = trainingOptions(‘adam’, … %%adam
‘MaxEpochs’,maxepochs, …
‘GradientThreshold’,1, …
‘InitialLearnRate’,0.005, …
‘LearnRateSchedule’,’piecewise’, …
‘LearnRateDropPeriod’,125, …
‘LearnRateDropFactor’,0.2, …
‘Verbose’,1, …
‘Plots’,’training-progress’);
%%Train the Network
net = trainNetwork(XTrain,YTrain,layers,options);
% YTest = data_mat(0.85*length(data_mat):end,1)’;
% XTest = data_mat(0.85*length(data_mat):end,1)’;
%
% XTest = num2cell(XTest,1);
% YTest = num2cell(YTest,1);
%
% net = resetState(net);
% YPred = predict(net,XTest)
%
%
% y1 = (cell2mat(YPred(1:end, 1:end))); %have to transpose as plot plots columns
% plot(y1)
% hold on
% y2 = (cell2mat(YTest(1:end, 1:end))’);
% plot(y2)Hi
I have this following code and the data set is traffic flow that gathered every 15 seconds and I want to predict data flow but i faced an error "Invalid training data. Responses must be a matrix of numeric responses, or a N-by-1 cell array of sequences, where N is the number of sequences. The feature dimension of all sequences must be the same"and I can’t fix it can anyone help me with it
My code is :
clc
close all
clear all
warning off
[~,~,flow_data] = xlsread(‘two_days.xlsx’); % Here we have two days data
data_mat = cell2mat(flow_data(2:end,3:4));
YTrain = data_mat(:,:)’;
XTrain = data_mat(:,:)’;
%
% %
XTrain = num2cell(XTrain,1)’;
YTrain = num2cell(YTrain,1)’;
numResponses = 1 ;
% numResponses = size(YTrain{1},1);
featureDimension = size(XTrain{1},1);
numHiddenUnits = 100;
layers = [ …
sequenceInputLayer(featureDimension)
lstmLayer(numHiddenUnits)
% dropoutLayer(0.1) %%0.5
fullyConnectedLayer(numResponses)
regressionLayer];
maxepochs = 500;
miniBatchSize = 1;
options = trainingOptions(‘adam’, … %%adam
‘MaxEpochs’,maxepochs, …
‘GradientThreshold’,1, …
‘InitialLearnRate’,0.005, …
‘LearnRateSchedule’,’piecewise’, …
‘LearnRateDropPeriod’,125, …
‘LearnRateDropFactor’,0.2, …
‘Verbose’,1, …
‘Plots’,’training-progress’);
%%Train the Network
net = trainNetwork(XTrain,YTrain,layers,options);
% YTest = data_mat(0.85*length(data_mat):end,1)’;
% XTest = data_mat(0.85*length(data_mat):end,1)’;
%
% XTest = num2cell(XTest,1);
% YTest = num2cell(YTest,1);
%
% net = resetState(net);
% YPred = predict(net,XTest)
%
%
% y1 = (cell2mat(YPred(1:end, 1:end))); %have to transpose as plot plots columns
% plot(y1)
% hold on
% y2 = (cell2mat(YTest(1:end, 1:end))’);
% plot(y2) Hi
I have this following code and the data set is traffic flow that gathered every 15 seconds and I want to predict data flow but i faced an error "Invalid training data. Responses must be a matrix of numeric responses, or a N-by-1 cell array of sequences, where N is the number of sequences. The feature dimension of all sequences must be the same"and I can’t fix it can anyone help me with it
My code is :
clc
close all
clear all
warning off
[~,~,flow_data] = xlsread(‘two_days.xlsx’); % Here we have two days data
data_mat = cell2mat(flow_data(2:end,3:4));
YTrain = data_mat(:,:)’;
XTrain = data_mat(:,:)’;
%
% %
XTrain = num2cell(XTrain,1)’;
YTrain = num2cell(YTrain,1)’;
numResponses = 1 ;
% numResponses = size(YTrain{1},1);
featureDimension = size(XTrain{1},1);
numHiddenUnits = 100;
layers = [ …
sequenceInputLayer(featureDimension)
lstmLayer(numHiddenUnits)
% dropoutLayer(0.1) %%0.5
fullyConnectedLayer(numResponses)
regressionLayer];
maxepochs = 500;
miniBatchSize = 1;
options = trainingOptions(‘adam’, … %%adam
‘MaxEpochs’,maxepochs, …
‘GradientThreshold’,1, …
‘InitialLearnRate’,0.005, …
‘LearnRateSchedule’,’piecewise’, …
‘LearnRateDropPeriod’,125, …
‘LearnRateDropFactor’,0.2, …
‘Verbose’,1, …
‘Plots’,’training-progress’);
%%Train the Network
net = trainNetwork(XTrain,YTrain,layers,options);
% YTest = data_mat(0.85*length(data_mat):end,1)’;
% XTest = data_mat(0.85*length(data_mat):end,1)’;
%
% XTest = num2cell(XTest,1);
% YTest = num2cell(YTest,1);
%
% net = resetState(net);
% YPred = predict(net,XTest)
%
%
% y1 = (cell2mat(YPred(1:end, 1:end))); %have to transpose as plot plots columns
% plot(y1)
% hold on
% y2 = (cell2mat(YTest(1:end, 1:end))’);
% plot(y2) trainnetwork, lstm, data MATLAB Answers — New Questions
Section color no longer an option in 2021b?
Prior to the changeover (versions 2021a and older), the current section in the Editor would be highlighted a yellowish color. Now with 2021b, there is a blue bar on the left to indicate the current section. I’d like the yellow-highlighted color back. Looking at the forum, there was an option in Preferences -> Colors -> Programming Tools that would allow the user to customize this section color, but unfortunately this has been removed. Or am I not seeing it somewhere?Prior to the changeover (versions 2021a and older), the current section in the Editor would be highlighted a yellowish color. Now with 2021b, there is a blue bar on the left to indicate the current section. I’d like the yellow-highlighted color back. Looking at the forum, there was an option in Preferences -> Colors -> Programming Tools that would allow the user to customize this section color, but unfortunately this has been removed. Or am I not seeing it somewhere? Prior to the changeover (versions 2021a and older), the current section in the Editor would be highlighted a yellowish color. Now with 2021b, there is a blue bar on the left to indicate the current section. I’d like the yellow-highlighted color back. Looking at the forum, there was an option in Preferences -> Colors -> Programming Tools that would allow the user to customize this section color, but unfortunately this has been removed. Or am I not seeing it somewhere? section, color, preferences, programming tools MATLAB Answers — New Questions
Single struct as alternative to name-value pairs in user-defined function
Hello, is it possible when defining a function with name-value argument pairs to allow a user to pass the struct(s) that define these name-value pairs directly to the function?
For example, I can define
function [s1, s2] = TestStructArgs(s1, s2)
arguments
s1 (1,1) struct
s2 (1,1) struct
end
end
then call TestStructArgs(struct(‘a’, 1, ‘b’, 2), struct(‘x’, 3, ‘y’, 4))
Or, I can define
function [s1, s2] = TestStructArgs2(s1, s2)
arguments
s1.a (1,1) double
s1.b (1,1) double
s2.x (1,1) double
s2.y (1,1) double
end
end
and call TestStructArgs2(‘a’, 1, ‘b’, 2, ‘x’, 3, ‘y’, 4)
But what if I would also like to call the second example by passing whole structs, TestStructArgs2(struct(‘a’, 1, ‘b’, 2), struct(‘x’, 3, ‘y’, 4)) ? There doesn’t appear to be any ambiguity in allowing both approaches, if they’re not mixed-and-matched.
Further, some MATLAB built-in functions permit this e.g. the following runs just fine
>> opts = struct(‘Color’, ‘r’, ‘LineWidth’, 2);
>> x = linspace(0,1,200); y = cos(x);
>> plot(x, y, opts)
There is an answer on the messageboard from 2022 here which suggests adding the attribute StructExpand after arguments but this causes an error if I add it to the above example
Many thanks for any helpHello, is it possible when defining a function with name-value argument pairs to allow a user to pass the struct(s) that define these name-value pairs directly to the function?
For example, I can define
function [s1, s2] = TestStructArgs(s1, s2)
arguments
s1 (1,1) struct
s2 (1,1) struct
end
end
then call TestStructArgs(struct(‘a’, 1, ‘b’, 2), struct(‘x’, 3, ‘y’, 4))
Or, I can define
function [s1, s2] = TestStructArgs2(s1, s2)
arguments
s1.a (1,1) double
s1.b (1,1) double
s2.x (1,1) double
s2.y (1,1) double
end
end
and call TestStructArgs2(‘a’, 1, ‘b’, 2, ‘x’, 3, ‘y’, 4)
But what if I would also like to call the second example by passing whole structs, TestStructArgs2(struct(‘a’, 1, ‘b’, 2), struct(‘x’, 3, ‘y’, 4)) ? There doesn’t appear to be any ambiguity in allowing both approaches, if they’re not mixed-and-matched.
Further, some MATLAB built-in functions permit this e.g. the following runs just fine
>> opts = struct(‘Color’, ‘r’, ‘LineWidth’, 2);
>> x = linspace(0,1,200); y = cos(x);
>> plot(x, y, opts)
There is an answer on the messageboard from 2022 here which suggests adding the attribute StructExpand after arguments but this causes an error if I add it to the above example
Many thanks for any help Hello, is it possible when defining a function with name-value argument pairs to allow a user to pass the struct(s) that define these name-value pairs directly to the function?
For example, I can define
function [s1, s2] = TestStructArgs(s1, s2)
arguments
s1 (1,1) struct
s2 (1,1) struct
end
end
then call TestStructArgs(struct(‘a’, 1, ‘b’, 2), struct(‘x’, 3, ‘y’, 4))
Or, I can define
function [s1, s2] = TestStructArgs2(s1, s2)
arguments
s1.a (1,1) double
s1.b (1,1) double
s2.x (1,1) double
s2.y (1,1) double
end
end
and call TestStructArgs2(‘a’, 1, ‘b’, 2, ‘x’, 3, ‘y’, 4)
But what if I would also like to call the second example by passing whole structs, TestStructArgs2(struct(‘a’, 1, ‘b’, 2), struct(‘x’, 3, ‘y’, 4)) ? There doesn’t appear to be any ambiguity in allowing both approaches, if they’re not mixed-and-matched.
Further, some MATLAB built-in functions permit this e.g. the following runs just fine
>> opts = struct(‘Color’, ‘r’, ‘LineWidth’, 2);
>> x = linspace(0,1,200); y = cos(x);
>> plot(x, y, opts)
There is an answer on the messageboard from 2022 here which suggests adding the attribute StructExpand after arguments but this causes an error if I add it to the above example
Many thanks for any help arguments, struct, validation, name-value MATLAB Answers — New Questions
Difference in computational time
Hi all, I am working on a curve fitting project and I have a couple of curves to fit. For 2 of the curves, the fitting function involves an integration and I was told that I will need to make each of the data loop through the function (using the for loop). The below is my first code, which takes less than a second to complete.
tic
% Preparation
clear; clc
load Steady_State_Data.mat
%% Preamble
% Fundamental constants
h = 4.0135667696*10^-15; % units: eV/ Hz
c = 3*10^8; % SI units
% Clean up of data to select range of values
Absorbance = log10(T_substrate./T_sample);
e = E >= 0 & E <= 2.0; % returns boolean value of the indices that satisfies the logical condition defined above
A = find(e); % gives indices that are non-zero
N = length(A); % no. of elements that are non-zero
n = length(E) + 1;
% Data for fitting
E_p = E(n-N:n-1);
Abs = Absorbance(n-N:n-1) – min(Absorbance);
% Fitting function: Elliott’s Model for Steady State with non-parabolic factor
function F = EM_SS_wR(p, e_p)
for i = 1:numel(e_p)
E_p = e_p(i);
F(i) = p(1)*(2*pi*sqrt(p(4))/E_p)*(1/p(6))*…
(integral(@(E)sech(((E_p – E)./p(6)))*(1 + 10*p(5)*(E – p(3)) + …
126*(p(5))^2*(E – p(3))^2)/(1 – exp(-2*pi*sqrt(p(4)/(E – p(3))))), p(3), Inf, ‘ArrayValued’, 1)) + …
p(2)*((2*pi*p(4)^3/2)/E_p)*1/p(6)*(…
(1/1^3)*sech((E_p – p(3) + p(4)/1^2)./p(6)) + …
(1/2^3)*sech((E_p – p(3) + p(4)/2^2)./p(6)) + …
(1/3^3)*sech((E_p – p(3) + p(4)/3^2)./p(6)) + …
(1/4^3)*sech((E_p – p(3) + p(4)/4^2)./p(6)) + …
(1/5^3)*sech((E_p – p(3) + p(4)/5^2)./p(6)) + …
(1/6^3)*sech((E_p – p(3) + p(4)/6^2)./p(6)) + …
(1/7^3)*sech((E_p – p(3) + p(4)/7^2)./p(6)));
end
F = F(:);
end
% Carrier Contribution
function F = EM_SS_wR_CC(p, e_p)
for i = 1:numel(e_p)
E_p = e_p(i);
F(i) = p(1)*(2*pi*sqrt(p(4))/E_p)*(1/p(6))*…
(integral(@(E)sech(((E_p – E)./p(6)))*(1 + 10*p(5)*(E – p(3)) + …
126*(p(5))^2*(E – p(3))^2)/(1 – exp(-2*pi*sqrt(p(4)/(E – p(3))))), p(3), Inf, ‘ArrayValued’, 1));
end
F = F(:);
end
% Excitoninc Contribution
EM_SS_wR_EC = @(p, E_p) p(2)*((2*pi*p(4)^3/2)/E_p)*1/p(6)*(…
(1/1^3)*sech((E_p – p(3) + p(4)/1^2)./p(6)) + …
(1/2^3)*sech((E_p – p(3) + p(4)/2^2)./p(6)) + …
(1/3^3)*sech((E_p – p(3) + p(4)/3^2)./p(6)) + …
(1/4^3)*sech((E_p – p(3) + p(4)/4^2)./p(6)) + …
(1/5^3)*sech((E_p – p(3) + p(4)/5^2)./p(6)) + …
(1/6^3)*sech((E_p – p(3) + p(4)/6^2)./p(6)) + …
(1/7^3)*sech((E_p – p(3) + p(4)/7^2)./p(6)));
%% Curve fitting options
% Initial parameter guess and bounds
lb = [-Inf, -Inf, 0, 0, -Inf, -Inf]; ub = [Inf, Inf, 55, 0.030, Inf, Inf];
p0 = [0.13, 0.11, 1.4, 0.025, 0.12, 0.035]; % refer to the next line for their order
% p0 = [A1, A2, Eg, Eb, R, g]
% lsqcurvefit and choose between different algorithm that lsqcurvefit employs (3C1, comment those lines that are not choosen and uncomment the line that is choosen, if not, matlab will take the last line of "optim_lsq" by default)
optim_lsq = optimoptions(‘lsqcurvefit’, ‘Algorithm’, ‘levenberg-marquardt’, ‘MaxFunctionEvaluations’,1000, ‘MaxIterations’, 1000, ‘FunctionTolerance’,10^-9, ‘StepTolerance’, 10^-9);
% optim_lsq = optimoptions(‘lsqcurvefit’, ‘Algorithm’, ‘trust-region-reflective’, ‘MaxFunctionEvaluations’,1000, ‘MaxIterations’, ‘FunctionTolerance’,10^-9, ‘StepTolerance’, 10^-9);
% optim_lsq = optimoptions(‘lsqcurvefit’, ‘Algorithm’, ‘interior-point’, ‘MaxFunctionEvaluations’,1000, ‘MaxIterations’, 1000, ‘FunctionTolerance’,10^-9, ‘StepTolerance’, 10^-9);
% Solver for lsqcurvefit
[p, residualnorm, residual, exitflag, output, lambda, jacobian] = lsqcurvefit(@EM_SS_wR, p0, E_p, Abs, lb, ub);
%% Error bars calculation
ci = nlparci(p, residual, ‘Jacobian’, jacobian);
PCI = table(ci(:,1), p(:), ci(:,2),’VariableNames’,{‘CI 0.025′,’p’,’CI 0.975′});
Parameter_CI = table2array(PCI);
upper_bar = (Parameter_CI(:,3) – Parameter_CI(:,2))./2;
lower_bar = (Parameter_CI(:,2) – Parameter_CI(:,1))./2;
%% Parameter values (refer to command window)
p1 = p(1,1);
p2 = p(1,2);
p3 = p(1,3);
p4 = p(1,4);
p5 = p(1,5);
p6 = p(1,6);
e1 = lower_bar(1,1);
e2 = lower_bar(2,1);
e3 = lower_bar(3,1);
e4 = lower_bar(4,1);
e5 = lower_bar(5,1);
e6 = lower_bar(6,1);
X1 = [‘ A1 = ‘, num2str(p1), char(177), num2str(e1)];
X2 = [‘ A2 = ‘, num2str(p2), char(177), num2str(e2)];
X3 = [‘ Eg = ‘, num2str(p3), char(177), num2str(e3)];
X4 = [‘ Eb = ‘, num2str(p4), char(177), num2str(e4)];
X5 = [‘ R = ‘, num2str(p5), char(177), num2str(e5)];
X6 = [‘ g = ‘, num2str(p6), char(177), num2str(e6)];
disp(X1);
disp(X2);
disp(X3);
disp(X4);
disp(X5);
disp(X6);
% Table of parameter values
parameter_name = {‘A1’; ‘A2’; ‘Eg’; ‘Eb’; ‘R’; ‘g’};
parameter_values = [p1; p2; p3; p4; p5; p6];
error = [e1; e2; e3; e4; e5; e6];
T = table(parameter_values, error, ‘RowNames’,parameter_name);
disp(T)
%% Plot command
plot(E_p, Abs, ‘o’, ‘Color’,’b’) % scatter plot
hold on
plot(E_p, EM_SS_wR(p, E_p), ‘LineWidth’, 1.0, ‘Color’,’red’) % curve fitting
plot(E_p, EM_SS_wR_CC(p, E_p), ‘LineWidth’, 1.0,’Color’,’black’) % carrier contribution
plot(E_p, EM_SS_wR_EC(p, E_p), ‘LineWidth’, 1.0, ‘Color’,’green’) % excitonic contribution
% Table of parameter values
TString = evalc(‘disp(T)’);
TString = strrep(TString,'<strong>’,’bf’);
TString = strrep(TString,'</strong>’,’rm’);
TString = strrep(TString,’_’,’_’);
FixedWidth = get(0,’FixedWidthFontName’);
annotation(gcf,’Textbox’,’String’,TString,’Interpreter’,’tex’,…
‘FontName’,FixedWidth,’Units’,’Normalized’,’Position’,[0.15, 0.8, 0.1, 0.1]);
xlabel(‘Probe energy (eV)’)
ylabel(‘Absorbance.O.D’)
legend(‘Experimental Data’, ‘Fitted Curve’, ‘Carrier Contribution’,’Excitonic Contribution’, ‘Location’,’southeast’)
hold off
%% Miscellaneous
% For a rectangular pic:
% drag horizontally until it just covers the letter ‘c’ in the word ‘col’
% For a square pic:
% drag horizontall until it just covers the letter ‘o’ in the word ‘Contribution’ in the next line)
% legend(‘Experimental Data’, ‘Fitted Curve’, ‘Carrier Contribution’,’Excitonic Contribution’)
toc
However, when using my second code (shown below) it takes around 5 minutes to complete running. May I please ask for the difference in the computational time?
tic
%% Preparation
clc; clear
data = importdata("FCPIB-293K-2.5mW-400nm-Jan072021 -ibg -bg -chirp.csv"); % insert file path within parenthesis
load Steady_State_Parameter_Values.mat
%% 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 = data(:, 1);% units: nm
E = (h*c)./(Wavelength*10^-9);
delay_t = data(1, :);
% Parameter values from Steady state fiting
A1 = p(1,1);
A2 = p(1,2);
Eg = p(1,3);
Eb = p(1,4);
R = p(1,5);
g = p(1,6);
t1 = 0.5342;
t2 = 1.0257;
t3 = 1.9673;
t4 = 3.9554;
carrier_T = [1198.8, 816.7, 446.8, 328.7];
col1 = 56;
col2 = 63;
col3 = 74;
col4 = 87;
% Data for fitting
Range_E = E >= 1.5 & E <= 2.0;
Range_W = Wavelength >= (h*c)/(2.0*10^-9) & Wavelength <= (h*c)/(1.5*10^-9);
E_p = E(Range_E); % selected probe energies
data_new2 = data(Range_W, [col1,col2,col3,col4]);
delta_Abs1 = data(Range_W,col1);
delta_Abs2 = data(Range_W,col2);
delta_Abs3 = data(Range_W,col3);
delta_Abs4 = data(Range_W,col4);
% Fitting function: Elliott’s Model for Transient Absorption
function F = EM_TA_wR1(x, e_p)
load Steady_State_Parameter_Values.mat
kB = 8.617333268*10^-5; % units: eV/ K
A1 = p(1,1);
A2 = p(1,2);
Eg = p(1,3);
Eb = p(1,4);
R = p(1,5);
g = p(1,6);
carrier_T = [1198.8, 816.7, 446.8, 328.7];
n = 1;
for i = 1:numel(e_p)
E_p = e_p(i);
F(i) = x(5)*A1*((2*pi*sqrt(x(2)))/E_p)*1/g*(integral(@(E)sech(((E_p – E)./g))*(1 + 10*R*(E – x(1)) + …
126*(R)^2*(E – x(1))^2)/(1 – exp(-2*pi*sqrt(x(2)/(E – x(1))))), x(1), Inf, ‘ArrayValued’, 1))*(1 – 1/(1 + exp((E_p – x(4))/(kB*carrier_T(1, n)))))^2 – …
A1*(2*pi*sqrt(Eb)/E_p)*(1/g)*…
(integral(@(e)sech(((E_p – e)./g))*(1 + 10*R*(e – Eg) + …
126*(R)^2*(e – Eg)^2)/(1 – exp(-2*pi*sqrt(Eb/(e – Eg)))), Eg, Inf, ‘ArrayValued’, 1)) + …
x(6)*A2*(4*pi*(x(2))^3/2)./(E_p).*1/x(3)*(…
(1/1^3)*sech((E_p – x(1) + x(2)/1^2)./x(3)) + …
(1/2^3)*sech((E_p – x(1) + x(2)/2^2)./x(3)) + …
(1/3^3)*sech((E_p – x(1) + x(2)/3^2)./x(3)) + …
(1/4^3)*sech((E_p – x(1) + x(2)/4^2)./x(3)) + …
(1/5^3)*sech((E_p – x(1) + x(2)/5^2)./x(3)) + …
(1/6^3)*sech((E_p – x(1) + x(2)/6^2)./x(3)) + …
(1/7^3)*sech((E_p – x(1) + x(2)/7^2)./x(3))) – …
A2*(4*pi*Eb^3/2)./E_p.*1/g*(…
(1/1^3)*sech((E_p – Eg + Eb/1^2)./g) + …
(1/2^3)*sech((E_p – Eg + Eb/2^2)./g) + …
(1/3^3)*sech((E_p – Eg + Eb/3^2)./g) + …
(1/4^3)*sech((E_p – Eg + Eb/4^2)./g) + …
(1/5^3)*sech((E_p – Eg + Eb/5^2)./g) + …
(1/6^3)*sech((E_p – Eg + Eb/6^2)./g) + …
(1/7^3)*sech((E_p – Eg + Eb/7^2)./g));
end
F = F(:);
end
function F = EM_TA_wR2(x, e_p)
data = importdata("Experimental dataTransient AbsorptionFCPIB-293K-2.5mW-400nm-Jan072021 -ibg -bg -chirp.csv"); % insert file path within parenthesis
load Steady_State_Parameter_Values.mat
h = 4.1356677*10^-15; % units: eV/ Hz
c = 3*10^8; % SI units
kB = 8.617333268*10^-5; % units: eV/ K
Wavelength = data(:, 1);% units: nm
E = (h*c)./(Wavelength*10^-9);
A1 = p(1,1);
A2 = p(1,2);
Eg = p(1,3);
Eb = p(1,4);
R = p(1,5);
g = p(1,6);
col1 = 56;
col2 = 63;
col3 = 74;
col4 = 87;
Range_E = E >= 1.5 & E <= 2.0;
Range_W = Wavelength >= (h*c)/(2.0*10^-9) & Wavelength <= (h*c)/(1.5*10^-9);
E_p = E(Range_E); % selected probe energies
data_new2 = data(Range_W, [col1,col2,col3,col4]);
carrier_T = [1198.8, 816.7, 446.8, 328.7];
n = 2;
for i = 1:numel(e_p)
E_p = e_p(i);
F(i) = x(5)*A1*((2*pi*sqrt(x(2)))/E_p)*1/g*(integral(@(E)sech(((E_p – E)./g))*(1 + 10*R*(E – x(1)) + …
126*(R)^2*(E – x(1))^2)/(1 – exp(-2*pi*sqrt(x(2)/(E – x(1))))), x(1), Inf, ‘ArrayValued’, 1))*(1 – 1/(1 + exp((E_p – x(4))/(kB*carrier_T(1, n)))))^2 – …
A1*(2*pi*sqrt(Eb)/E_p)*(1/g)*…
(integral(@(e)sech(((E_p – e)./g))*(1 + 10*R*(e – Eg) + …
126*(R)^2*(e – Eg)^2)/(1 – exp(-2*pi*sqrt(Eb/(e – Eg)))), Eg, Inf, ‘ArrayValued’, 1)) + …
x(6)*A2*(4*pi*(x(2))^3/2)*1/x(3)*(…
(1/1^3)*sech((E_p – x(1) + x(2)/1^2)./x(3)) + …
(1/2^3)*sech((E_p – x(1) + x(2)/2^2)./x(3)) + …
(1/3^3)*sech((E_p – x(1) + x(2)/3^2)./x(3)) + …
(1/4^3)*sech((E_p – x(1) + x(2)/4^2)./x(3)) + …
(1/5^3)*sech((E_p – x(1) + x(2)/5^2)./x(3)) + …
(1/6^3)*sech((E_p – x(1) + x(2)/6^2)./x(3)) + …
(1/7^3)*sech((E_p – x(1) + x(2)/7^2)./x(3))) – …
A2*(4*pi*Eb^3/2)*1/g*(…
(1/1^3)*sech((E_p – Eg + Eb/1^2)./g) + …
(1/2^3)*sech((E_p – Eg + Eb/2^2)./g) + …
(1/3^3)*sech((E_p – Eg + Eb/3^2)./g) + …
(1/4^3)*sech((E_p – Eg + Eb/4^2)./g) + …
(1/5^3)*sech((E_p – Eg + Eb/5^2)./g) + …
(1/6^3)*sech((E_p – Eg + Eb/6^2)./g) + …
(1/7^3)*sech((E_p – Eg + Eb/7^2)./g));
end
F = F(:);
end
function F = EM_TA_wR3(x, e_p)
load Steady_State_Parameter_Values.mat
h = 4.1356677*10^-15; % units: eV/ Hz
c = 3*10^8; % SI units
kB = 8.617333268*10^-5; % units: eV/ K
Wavelength = data(:, 1);% units: nm
E = (h*c)./(Wavelength*10^-9);
A1 = p(1,1);
A2 = p(1,2);
Eg = p(1,3);
Eb = p(1,4);
R = p(1,5);
g = p(1,6);
col1 = 56;
col2 = 63;
col3 = 74;
col4 = 87;
Range_E = E >= 1.5 & E <= 2.0;
Range_W = Wavelength >= (h*c)/(2.0*10^-9) & Wavelength <= (h*c)/(1.5*10^-9);
E_p = E(Range_E); % selected probe energies
data_new2 = data(Range_W, [col1,col2,col3,col4]);
carrier_T = [1198.8, 816.7, 446.8, 328.7];
n = 3;
for i = 1:numel(e_p)
E_p = e_p(i);
F(i) = x(5)*A1*((2*pi*sqrt(x(2)))/E_p)*1/g*(integral(@(E)sech(((E_p – E)./g))*(1 + 10*R*(E – x(1)) + …
126*(R)^2*(E – x(1))^2)/(1 – exp(-2*pi*sqrt(x(2)/(E – x(1))))), x(1), Inf, ‘ArrayValued’, 1))*(1 – 1/(1 + exp((E_p – x(4))/(kB*carrier_T(1, n)))))^2 – …
A1*(2*pi*sqrt(Eb)/E_p)*(1/g)*…
(integral(@(e)sech(((E_p – e)./g))*(1 + 10*R*(e – Eg) + …
126*(R)^2*(e – Eg)^2)/(1 – exp(-2*pi*sqrt(Eb/(e – Eg)))), Eg, Inf, ‘ArrayValued’, 1)) + …
x(6)*A2*(4*pi*(x(2))^3/2)*1/x(3)*(…
(1/1^3)*sech((E_p – x(1) + x(2)/1^2)./x(3)) + …
(1/2^3)*sech((E_p – x(1) + x(2)/2^2)./x(3)) + …
(1/3^3)*sech((E_p – x(1) + x(2)/3^2)./x(3)) + …
(1/4^3)*sech((E_p – x(1) + x(2)/4^2)./x(3)) + …
(1/5^3)*sech((E_p – x(1) + x(2)/5^2)./x(3)) + …
(1/6^3)*sech((E_p – x(1) + x(2)/6^2)./x(3)) + …
(1/7^3)*sech((E_p – x(1) + x(2)/7^2)./x(3))) – …
A2*(4*pi*Eb^3/2)*1/g*(…
(1/1^3)*sech((E_p – Eg + Eb/1^2)./g) + …
(1/2^3)*sech((E_p – Eg + Eb/2^2)./g) + …
(1/3^3)*sech((E_p – Eg + Eb/3^2)./g) + …
(1/4^3)*sech((E_p – Eg + Eb/4^2)./g) + …
(1/5^3)*sech((E_p – Eg + Eb/5^2)./g) + …
(1/6^3)*sech((E_p – Eg + Eb/6^2)./g) + …
(1/7^3)*sech((E_p – Eg + Eb/7^2)./g));
end
F = F(:);
end
function F = EM_TA_wR4(x, e_p)
load Steady_State_Parameter_Values.mat
h = 4.1356677*10^-15; % units: eV/ Hz
c = 3*10^8; % SI units
kB = 8.617333268*10^-5; % units: eV/ K
Wavelength = data(:, 1);% units: nm
E = (h*c)./(Wavelength*10^-9);
A1 = p(1,1);
A2 = p(1,2);
Eg = p(1,3);
Eb = p(1,4);
R = p(1,5);
g = p(1,6);
col1 = 56;
col2 = 63;
col3 = 74;
col4 = 87;
Range_E = E >= 1.5 & E <= 2.0;
Range_W = Wavelength >= (h*c)/(2.0*10^-9) & Wavelength <= (h*c)/(1.5*10^-9);
E_p = E(Range_E); % selected probe energies
data_new2 = data(Range_W, [col1,col2,col3,col4]);
carrier_T = [1198.8, 816.7, 446.8, 328.7];
n = 4;
for i = 1:numel(e_p)
E_p = e_p(i);
F(i) = x(5)*A1*((2*pi*sqrt(x(2)))/E_p)*1/g*(integral(@(E)sech(((E_p – E)./g))*(1 + 10*R*(E – x(1)) + …
126*(R)^2*(E – x(1))^2)/(1 – exp(-2*pi*sqrt(x(2)/(E – x(1))))), x(1), Inf, ‘ArrayValued’, 1))*(1 – 1/(1 + exp((E_p – x(4))/(kB*carrier_T(1, n)))))^2 – …
A1*(2*pi*sqrt(Eb)/E_p)*(1/g)*…
(integral(@(e)sech(((E_p – e)./g))*(1 + 10*R*(e – Eg) + …
126*(R)^2*(e – Eg)^2)/(1 – exp(-2*pi*sqrt(Eb/(e – Eg)))), Eg, Inf, ‘ArrayValued’, 1)) + …
x(6)*A2*(4*pi*(x(2))^3/2)*1/x(3)*(…
(1/1^3)*sech((E_p – x(1) + x(2)/1^2)./x(3)) + …
(1/2^3)*sech((E_p – x(1) + x(2)/2^2)./x(3)) + …
(1/3^3)*sech((E_p – x(1) + x(2)/3^2)./x(3)) + …
(1/4^3)*sech((E_p – x(1) + x(2)/4^2)./x(3)) + …
(1/5^3)*sech((E_p – x(1) + x(2)/5^2)./x(3)) + …
(1/6^3)*sech((E_p – x(1) + x(2)/6^2)./x(3)) + …
(1/7^3)*sech((E_p – x(1) + x(2)/7^2)./x(3))) – …
A2*(4*pi*Eb^3/2)*1/g*(…
(1/1^3)*sech((E_p – Eg + Eb/1^2)./g) + …
(1/2^3)*sech((E_p – Eg + Eb/2^2)./g) + …
(1/3^3)*sech((E_p – Eg + Eb/3^2)./g) + …
(1/4^3)*sech((E_p – Eg + Eb/4^2)./g) + …
(1/5^3)*sech((E_p – Eg + Eb/5^2)./g) + …
(1/6^3)*sech((E_p – Eg + Eb/6^2)./g) + …
(1/7^3)*sech((E_p – Eg + Eb/7^2)./g));
end
F = F(:);
end
% Curve fitting
lb = [Eg, Eb, g, 0.3, 0.5, 0.2]; ub = [55, 0.05, 0.05, 20, 2, 1];
x0 = [1.65, 0.03, 0.03, 1.3, 1, 0.3];
optim_lsq = optimoptions(‘lsqcurvefit’, ‘Algorithm’, ‘levenberg-marquardt’, ‘MaxFunctionEvaluations’,10^5, ‘MaxIterations’, 10^5, ‘FunctionTolerance’,10^-20, ‘StepTolerance’, 10^-20);
delta_Abs(:,1)= data_new2(:,1);
x1 = lsqcurvefit(@EM_TA_wR1, x0, E_p, delta_Abs1, lb, ub, optim_lsq);
x2 = lsqcurvefit(@EM_TA_wR2, x1, E_p, delta_Abs2, lb, ub, optim_lsq);
x3 = lsqcurvefit(@EM_TA_wR3, x2, E_p, delta_Abs3, lb, ub, optim_lsq);
x4 = lsqcurvefit(@EM_TA_wR4, x3, E_p, delta_Abs4, lb, ub, optim_lsq);
plot(E_p, delta_Abs1, ‘o’,’color’,’blue’)
hold on
plot(E_p, delta_Abs2, ‘o’, ‘color’, ‘yellow’)
plot(E_p, delta_Abs3, ‘o’, ‘color’, ‘green’)
plot(E_p, delta_Abs4, ‘o’, ‘color’, ‘magenta’)
plot(E_p, EM_TA_wR1(x1, E_p), ‘color’, ‘blue’, ‘LineWidth’, 4.0)
plot(E_p, EM_TA_wR2(x2, E_p), ‘color’, ‘yellow’, ‘LineWidth’, 4.0)
plot(E_p, EM_TA_wR3(x3, E_p), ‘color’, ‘green’, ‘LineWidth’, 4.0)
plot(E_p, EM_TA_wR4(x4, E_p), ‘color’, ‘magenta’, ‘LineWidth’, 4.0)
xlabel(‘Probe Photon Energy (eV)’)
ylabel(‘Delta A (O.D.)’)
legend(‘0.5 ps’, ‘1.0 ps’, ‘2.0 ps’, ‘4.0 ps’, ‘Location’, ‘southeast’)
hold off
disp(x1)
disp(x2)
disp(x3)
disp(x4)
tocHi all, I am working on a curve fitting project and I have a couple of curves to fit. For 2 of the curves, the fitting function involves an integration and I was told that I will need to make each of the data loop through the function (using the for loop). The below is my first code, which takes less than a second to complete.
tic
% Preparation
clear; clc
load Steady_State_Data.mat
%% Preamble
% Fundamental constants
h = 4.0135667696*10^-15; % units: eV/ Hz
c = 3*10^8; % SI units
% Clean up of data to select range of values
Absorbance = log10(T_substrate./T_sample);
e = E >= 0 & E <= 2.0; % returns boolean value of the indices that satisfies the logical condition defined above
A = find(e); % gives indices that are non-zero
N = length(A); % no. of elements that are non-zero
n = length(E) + 1;
% Data for fitting
E_p = E(n-N:n-1);
Abs = Absorbance(n-N:n-1) – min(Absorbance);
% Fitting function: Elliott’s Model for Steady State with non-parabolic factor
function F = EM_SS_wR(p, e_p)
for i = 1:numel(e_p)
E_p = e_p(i);
F(i) = p(1)*(2*pi*sqrt(p(4))/E_p)*(1/p(6))*…
(integral(@(E)sech(((E_p – E)./p(6)))*(1 + 10*p(5)*(E – p(3)) + …
126*(p(5))^2*(E – p(3))^2)/(1 – exp(-2*pi*sqrt(p(4)/(E – p(3))))), p(3), Inf, ‘ArrayValued’, 1)) + …
p(2)*((2*pi*p(4)^3/2)/E_p)*1/p(6)*(…
(1/1^3)*sech((E_p – p(3) + p(4)/1^2)./p(6)) + …
(1/2^3)*sech((E_p – p(3) + p(4)/2^2)./p(6)) + …
(1/3^3)*sech((E_p – p(3) + p(4)/3^2)./p(6)) + …
(1/4^3)*sech((E_p – p(3) + p(4)/4^2)./p(6)) + …
(1/5^3)*sech((E_p – p(3) + p(4)/5^2)./p(6)) + …
(1/6^3)*sech((E_p – p(3) + p(4)/6^2)./p(6)) + …
(1/7^3)*sech((E_p – p(3) + p(4)/7^2)./p(6)));
end
F = F(:);
end
% Carrier Contribution
function F = EM_SS_wR_CC(p, e_p)
for i = 1:numel(e_p)
E_p = e_p(i);
F(i) = p(1)*(2*pi*sqrt(p(4))/E_p)*(1/p(6))*…
(integral(@(E)sech(((E_p – E)./p(6)))*(1 + 10*p(5)*(E – p(3)) + …
126*(p(5))^2*(E – p(3))^2)/(1 – exp(-2*pi*sqrt(p(4)/(E – p(3))))), p(3), Inf, ‘ArrayValued’, 1));
end
F = F(:);
end
% Excitoninc Contribution
EM_SS_wR_EC = @(p, E_p) p(2)*((2*pi*p(4)^3/2)/E_p)*1/p(6)*(…
(1/1^3)*sech((E_p – p(3) + p(4)/1^2)./p(6)) + …
(1/2^3)*sech((E_p – p(3) + p(4)/2^2)./p(6)) + …
(1/3^3)*sech((E_p – p(3) + p(4)/3^2)./p(6)) + …
(1/4^3)*sech((E_p – p(3) + p(4)/4^2)./p(6)) + …
(1/5^3)*sech((E_p – p(3) + p(4)/5^2)./p(6)) + …
(1/6^3)*sech((E_p – p(3) + p(4)/6^2)./p(6)) + …
(1/7^3)*sech((E_p – p(3) + p(4)/7^2)./p(6)));
%% Curve fitting options
% Initial parameter guess and bounds
lb = [-Inf, -Inf, 0, 0, -Inf, -Inf]; ub = [Inf, Inf, 55, 0.030, Inf, Inf];
p0 = [0.13, 0.11, 1.4, 0.025, 0.12, 0.035]; % refer to the next line for their order
% p0 = [A1, A2, Eg, Eb, R, g]
% lsqcurvefit and choose between different algorithm that lsqcurvefit employs (3C1, comment those lines that are not choosen and uncomment the line that is choosen, if not, matlab will take the last line of "optim_lsq" by default)
optim_lsq = optimoptions(‘lsqcurvefit’, ‘Algorithm’, ‘levenberg-marquardt’, ‘MaxFunctionEvaluations’,1000, ‘MaxIterations’, 1000, ‘FunctionTolerance’,10^-9, ‘StepTolerance’, 10^-9);
% optim_lsq = optimoptions(‘lsqcurvefit’, ‘Algorithm’, ‘trust-region-reflective’, ‘MaxFunctionEvaluations’,1000, ‘MaxIterations’, ‘FunctionTolerance’,10^-9, ‘StepTolerance’, 10^-9);
% optim_lsq = optimoptions(‘lsqcurvefit’, ‘Algorithm’, ‘interior-point’, ‘MaxFunctionEvaluations’,1000, ‘MaxIterations’, 1000, ‘FunctionTolerance’,10^-9, ‘StepTolerance’, 10^-9);
% Solver for lsqcurvefit
[p, residualnorm, residual, exitflag, output, lambda, jacobian] = lsqcurvefit(@EM_SS_wR, p0, E_p, Abs, lb, ub);
%% Error bars calculation
ci = nlparci(p, residual, ‘Jacobian’, jacobian);
PCI = table(ci(:,1), p(:), ci(:,2),’VariableNames’,{‘CI 0.025′,’p’,’CI 0.975′});
Parameter_CI = table2array(PCI);
upper_bar = (Parameter_CI(:,3) – Parameter_CI(:,2))./2;
lower_bar = (Parameter_CI(:,2) – Parameter_CI(:,1))./2;
%% Parameter values (refer to command window)
p1 = p(1,1);
p2 = p(1,2);
p3 = p(1,3);
p4 = p(1,4);
p5 = p(1,5);
p6 = p(1,6);
e1 = lower_bar(1,1);
e2 = lower_bar(2,1);
e3 = lower_bar(3,1);
e4 = lower_bar(4,1);
e5 = lower_bar(5,1);
e6 = lower_bar(6,1);
X1 = [‘ A1 = ‘, num2str(p1), char(177), num2str(e1)];
X2 = [‘ A2 = ‘, num2str(p2), char(177), num2str(e2)];
X3 = [‘ Eg = ‘, num2str(p3), char(177), num2str(e3)];
X4 = [‘ Eb = ‘, num2str(p4), char(177), num2str(e4)];
X5 = [‘ R = ‘, num2str(p5), char(177), num2str(e5)];
X6 = [‘ g = ‘, num2str(p6), char(177), num2str(e6)];
disp(X1);
disp(X2);
disp(X3);
disp(X4);
disp(X5);
disp(X6);
% Table of parameter values
parameter_name = {‘A1’; ‘A2’; ‘Eg’; ‘Eb’; ‘R’; ‘g’};
parameter_values = [p1; p2; p3; p4; p5; p6];
error = [e1; e2; e3; e4; e5; e6];
T = table(parameter_values, error, ‘RowNames’,parameter_name);
disp(T)
%% Plot command
plot(E_p, Abs, ‘o’, ‘Color’,’b’) % scatter plot
hold on
plot(E_p, EM_SS_wR(p, E_p), ‘LineWidth’, 1.0, ‘Color’,’red’) % curve fitting
plot(E_p, EM_SS_wR_CC(p, E_p), ‘LineWidth’, 1.0,’Color’,’black’) % carrier contribution
plot(E_p, EM_SS_wR_EC(p, E_p), ‘LineWidth’, 1.0, ‘Color’,’green’) % excitonic contribution
% Table of parameter values
TString = evalc(‘disp(T)’);
TString = strrep(TString,'<strong>’,’bf’);
TString = strrep(TString,'</strong>’,’rm’);
TString = strrep(TString,’_’,’_’);
FixedWidth = get(0,’FixedWidthFontName’);
annotation(gcf,’Textbox’,’String’,TString,’Interpreter’,’tex’,…
‘FontName’,FixedWidth,’Units’,’Normalized’,’Position’,[0.15, 0.8, 0.1, 0.1]);
xlabel(‘Probe energy (eV)’)
ylabel(‘Absorbance.O.D’)
legend(‘Experimental Data’, ‘Fitted Curve’, ‘Carrier Contribution’,’Excitonic Contribution’, ‘Location’,’southeast’)
hold off
%% Miscellaneous
% For a rectangular pic:
% drag horizontally until it just covers the letter ‘c’ in the word ‘col’
% For a square pic:
% drag horizontall until it just covers the letter ‘o’ in the word ‘Contribution’ in the next line)
% legend(‘Experimental Data’, ‘Fitted Curve’, ‘Carrier Contribution’,’Excitonic Contribution’)
toc
However, when using my second code (shown below) it takes around 5 minutes to complete running. May I please ask for the difference in the computational time?
tic
%% Preparation
clc; clear
data = importdata("FCPIB-293K-2.5mW-400nm-Jan072021 -ibg -bg -chirp.csv"); % insert file path within parenthesis
load Steady_State_Parameter_Values.mat
%% 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 = data(:, 1);% units: nm
E = (h*c)./(Wavelength*10^-9);
delay_t = data(1, :);
% Parameter values from Steady state fiting
A1 = p(1,1);
A2 = p(1,2);
Eg = p(1,3);
Eb = p(1,4);
R = p(1,5);
g = p(1,6);
t1 = 0.5342;
t2 = 1.0257;
t3 = 1.9673;
t4 = 3.9554;
carrier_T = [1198.8, 816.7, 446.8, 328.7];
col1 = 56;
col2 = 63;
col3 = 74;
col4 = 87;
% Data for fitting
Range_E = E >= 1.5 & E <= 2.0;
Range_W = Wavelength >= (h*c)/(2.0*10^-9) & Wavelength <= (h*c)/(1.5*10^-9);
E_p = E(Range_E); % selected probe energies
data_new2 = data(Range_W, [col1,col2,col3,col4]);
delta_Abs1 = data(Range_W,col1);
delta_Abs2 = data(Range_W,col2);
delta_Abs3 = data(Range_W,col3);
delta_Abs4 = data(Range_W,col4);
% Fitting function: Elliott’s Model for Transient Absorption
function F = EM_TA_wR1(x, e_p)
load Steady_State_Parameter_Values.mat
kB = 8.617333268*10^-5; % units: eV/ K
A1 = p(1,1);
A2 = p(1,2);
Eg = p(1,3);
Eb = p(1,4);
R = p(1,5);
g = p(1,6);
carrier_T = [1198.8, 816.7, 446.8, 328.7];
n = 1;
for i = 1:numel(e_p)
E_p = e_p(i);
F(i) = x(5)*A1*((2*pi*sqrt(x(2)))/E_p)*1/g*(integral(@(E)sech(((E_p – E)./g))*(1 + 10*R*(E – x(1)) + …
126*(R)^2*(E – x(1))^2)/(1 – exp(-2*pi*sqrt(x(2)/(E – x(1))))), x(1), Inf, ‘ArrayValued’, 1))*(1 – 1/(1 + exp((E_p – x(4))/(kB*carrier_T(1, n)))))^2 – …
A1*(2*pi*sqrt(Eb)/E_p)*(1/g)*…
(integral(@(e)sech(((E_p – e)./g))*(1 + 10*R*(e – Eg) + …
126*(R)^2*(e – Eg)^2)/(1 – exp(-2*pi*sqrt(Eb/(e – Eg)))), Eg, Inf, ‘ArrayValued’, 1)) + …
x(6)*A2*(4*pi*(x(2))^3/2)./(E_p).*1/x(3)*(…
(1/1^3)*sech((E_p – x(1) + x(2)/1^2)./x(3)) + …
(1/2^3)*sech((E_p – x(1) + x(2)/2^2)./x(3)) + …
(1/3^3)*sech((E_p – x(1) + x(2)/3^2)./x(3)) + …
(1/4^3)*sech((E_p – x(1) + x(2)/4^2)./x(3)) + …
(1/5^3)*sech((E_p – x(1) + x(2)/5^2)./x(3)) + …
(1/6^3)*sech((E_p – x(1) + x(2)/6^2)./x(3)) + …
(1/7^3)*sech((E_p – x(1) + x(2)/7^2)./x(3))) – …
A2*(4*pi*Eb^3/2)./E_p.*1/g*(…
(1/1^3)*sech((E_p – Eg + Eb/1^2)./g) + …
(1/2^3)*sech((E_p – Eg + Eb/2^2)./g) + …
(1/3^3)*sech((E_p – Eg + Eb/3^2)./g) + …
(1/4^3)*sech((E_p – Eg + Eb/4^2)./g) + …
(1/5^3)*sech((E_p – Eg + Eb/5^2)./g) + …
(1/6^3)*sech((E_p – Eg + Eb/6^2)./g) + …
(1/7^3)*sech((E_p – Eg + Eb/7^2)./g));
end
F = F(:);
end
function F = EM_TA_wR2(x, e_p)
data = importdata("Experimental dataTransient AbsorptionFCPIB-293K-2.5mW-400nm-Jan072021 -ibg -bg -chirp.csv"); % insert file path within parenthesis
load Steady_State_Parameter_Values.mat
h = 4.1356677*10^-15; % units: eV/ Hz
c = 3*10^8; % SI units
kB = 8.617333268*10^-5; % units: eV/ K
Wavelength = data(:, 1);% units: nm
E = (h*c)./(Wavelength*10^-9);
A1 = p(1,1);
A2 = p(1,2);
Eg = p(1,3);
Eb = p(1,4);
R = p(1,5);
g = p(1,6);
col1 = 56;
col2 = 63;
col3 = 74;
col4 = 87;
Range_E = E >= 1.5 & E <= 2.0;
Range_W = Wavelength >= (h*c)/(2.0*10^-9) & Wavelength <= (h*c)/(1.5*10^-9);
E_p = E(Range_E); % selected probe energies
data_new2 = data(Range_W, [col1,col2,col3,col4]);
carrier_T = [1198.8, 816.7, 446.8, 328.7];
n = 2;
for i = 1:numel(e_p)
E_p = e_p(i);
F(i) = x(5)*A1*((2*pi*sqrt(x(2)))/E_p)*1/g*(integral(@(E)sech(((E_p – E)./g))*(1 + 10*R*(E – x(1)) + …
126*(R)^2*(E – x(1))^2)/(1 – exp(-2*pi*sqrt(x(2)/(E – x(1))))), x(1), Inf, ‘ArrayValued’, 1))*(1 – 1/(1 + exp((E_p – x(4))/(kB*carrier_T(1, n)))))^2 – …
A1*(2*pi*sqrt(Eb)/E_p)*(1/g)*…
(integral(@(e)sech(((E_p – e)./g))*(1 + 10*R*(e – Eg) + …
126*(R)^2*(e – Eg)^2)/(1 – exp(-2*pi*sqrt(Eb/(e – Eg)))), Eg, Inf, ‘ArrayValued’, 1)) + …
x(6)*A2*(4*pi*(x(2))^3/2)*1/x(3)*(…
(1/1^3)*sech((E_p – x(1) + x(2)/1^2)./x(3)) + …
(1/2^3)*sech((E_p – x(1) + x(2)/2^2)./x(3)) + …
(1/3^3)*sech((E_p – x(1) + x(2)/3^2)./x(3)) + …
(1/4^3)*sech((E_p – x(1) + x(2)/4^2)./x(3)) + …
(1/5^3)*sech((E_p – x(1) + x(2)/5^2)./x(3)) + …
(1/6^3)*sech((E_p – x(1) + x(2)/6^2)./x(3)) + …
(1/7^3)*sech((E_p – x(1) + x(2)/7^2)./x(3))) – …
A2*(4*pi*Eb^3/2)*1/g*(…
(1/1^3)*sech((E_p – Eg + Eb/1^2)./g) + …
(1/2^3)*sech((E_p – Eg + Eb/2^2)./g) + …
(1/3^3)*sech((E_p – Eg + Eb/3^2)./g) + …
(1/4^3)*sech((E_p – Eg + Eb/4^2)./g) + …
(1/5^3)*sech((E_p – Eg + Eb/5^2)./g) + …
(1/6^3)*sech((E_p – Eg + Eb/6^2)./g) + …
(1/7^3)*sech((E_p – Eg + Eb/7^2)./g));
end
F = F(:);
end
function F = EM_TA_wR3(x, e_p)
load Steady_State_Parameter_Values.mat
h = 4.1356677*10^-15; % units: eV/ Hz
c = 3*10^8; % SI units
kB = 8.617333268*10^-5; % units: eV/ K
Wavelength = data(:, 1);% units: nm
E = (h*c)./(Wavelength*10^-9);
A1 = p(1,1);
A2 = p(1,2);
Eg = p(1,3);
Eb = p(1,4);
R = p(1,5);
g = p(1,6);
col1 = 56;
col2 = 63;
col3 = 74;
col4 = 87;
Range_E = E >= 1.5 & E <= 2.0;
Range_W = Wavelength >= (h*c)/(2.0*10^-9) & Wavelength <= (h*c)/(1.5*10^-9);
E_p = E(Range_E); % selected probe energies
data_new2 = data(Range_W, [col1,col2,col3,col4]);
carrier_T = [1198.8, 816.7, 446.8, 328.7];
n = 3;
for i = 1:numel(e_p)
E_p = e_p(i);
F(i) = x(5)*A1*((2*pi*sqrt(x(2)))/E_p)*1/g*(integral(@(E)sech(((E_p – E)./g))*(1 + 10*R*(E – x(1)) + …
126*(R)^2*(E – x(1))^2)/(1 – exp(-2*pi*sqrt(x(2)/(E – x(1))))), x(1), Inf, ‘ArrayValued’, 1))*(1 – 1/(1 + exp((E_p – x(4))/(kB*carrier_T(1, n)))))^2 – …
A1*(2*pi*sqrt(Eb)/E_p)*(1/g)*…
(integral(@(e)sech(((E_p – e)./g))*(1 + 10*R*(e – Eg) + …
126*(R)^2*(e – Eg)^2)/(1 – exp(-2*pi*sqrt(Eb/(e – Eg)))), Eg, Inf, ‘ArrayValued’, 1)) + …
x(6)*A2*(4*pi*(x(2))^3/2)*1/x(3)*(…
(1/1^3)*sech((E_p – x(1) + x(2)/1^2)./x(3)) + …
(1/2^3)*sech((E_p – x(1) + x(2)/2^2)./x(3)) + …
(1/3^3)*sech((E_p – x(1) + x(2)/3^2)./x(3)) + …
(1/4^3)*sech((E_p – x(1) + x(2)/4^2)./x(3)) + …
(1/5^3)*sech((E_p – x(1) + x(2)/5^2)./x(3)) + …
(1/6^3)*sech((E_p – x(1) + x(2)/6^2)./x(3)) + …
(1/7^3)*sech((E_p – x(1) + x(2)/7^2)./x(3))) – …
A2*(4*pi*Eb^3/2)*1/g*(…
(1/1^3)*sech((E_p – Eg + Eb/1^2)./g) + …
(1/2^3)*sech((E_p – Eg + Eb/2^2)./g) + …
(1/3^3)*sech((E_p – Eg + Eb/3^2)./g) + …
(1/4^3)*sech((E_p – Eg + Eb/4^2)./g) + …
(1/5^3)*sech((E_p – Eg + Eb/5^2)./g) + …
(1/6^3)*sech((E_p – Eg + Eb/6^2)./g) + …
(1/7^3)*sech((E_p – Eg + Eb/7^2)./g));
end
F = F(:);
end
function F = EM_TA_wR4(x, e_p)
load Steady_State_Parameter_Values.mat
h = 4.1356677*10^-15; % units: eV/ Hz
c = 3*10^8; % SI units
kB = 8.617333268*10^-5; % units: eV/ K
Wavelength = data(:, 1);% units: nm
E = (h*c)./(Wavelength*10^-9);
A1 = p(1,1);
A2 = p(1,2);
Eg = p(1,3);
Eb = p(1,4);
R = p(1,5);
g = p(1,6);
col1 = 56;
col2 = 63;
col3 = 74;
col4 = 87;
Range_E = E >= 1.5 & E <= 2.0;
Range_W = Wavelength >= (h*c)/(2.0*10^-9) & Wavelength <= (h*c)/(1.5*10^-9);
E_p = E(Range_E); % selected probe energies
data_new2 = data(Range_W, [col1,col2,col3,col4]);
carrier_T = [1198.8, 816.7, 446.8, 328.7];
n = 4;
for i = 1:numel(e_p)
E_p = e_p(i);
F(i) = x(5)*A1*((2*pi*sqrt(x(2)))/E_p)*1/g*(integral(@(E)sech(((E_p – E)./g))*(1 + 10*R*(E – x(1)) + …
126*(R)^2*(E – x(1))^2)/(1 – exp(-2*pi*sqrt(x(2)/(E – x(1))))), x(1), Inf, ‘ArrayValued’, 1))*(1 – 1/(1 + exp((E_p – x(4))/(kB*carrier_T(1, n)))))^2 – …
A1*(2*pi*sqrt(Eb)/E_p)*(1/g)*…
(integral(@(e)sech(((E_p – e)./g))*(1 + 10*R*(e – Eg) + …
126*(R)^2*(e – Eg)^2)/(1 – exp(-2*pi*sqrt(Eb/(e – Eg)))), Eg, Inf, ‘ArrayValued’, 1)) + …
x(6)*A2*(4*pi*(x(2))^3/2)*1/x(3)*(…
(1/1^3)*sech((E_p – x(1) + x(2)/1^2)./x(3)) + …
(1/2^3)*sech((E_p – x(1) + x(2)/2^2)./x(3)) + …
(1/3^3)*sech((E_p – x(1) + x(2)/3^2)./x(3)) + …
(1/4^3)*sech((E_p – x(1) + x(2)/4^2)./x(3)) + …
(1/5^3)*sech((E_p – x(1) + x(2)/5^2)./x(3)) + …
(1/6^3)*sech((E_p – x(1) + x(2)/6^2)./x(3)) + …
(1/7^3)*sech((E_p – x(1) + x(2)/7^2)./x(3))) – …
A2*(4*pi*Eb^3/2)*1/g*(…
(1/1^3)*sech((E_p – Eg + Eb/1^2)./g) + …
(1/2^3)*sech((E_p – Eg + Eb/2^2)./g) + …
(1/3^3)*sech((E_p – Eg + Eb/3^2)./g) + …
(1/4^3)*sech((E_p – Eg + Eb/4^2)./g) + …
(1/5^3)*sech((E_p – Eg + Eb/5^2)./g) + …
(1/6^3)*sech((E_p – Eg + Eb/6^2)./g) + …
(1/7^3)*sech((E_p – Eg + Eb/7^2)./g));
end
F = F(:);
end
% Curve fitting
lb = [Eg, Eb, g, 0.3, 0.5, 0.2]; ub = [55, 0.05, 0.05, 20, 2, 1];
x0 = [1.65, 0.03, 0.03, 1.3, 1, 0.3];
optim_lsq = optimoptions(‘lsqcurvefit’, ‘Algorithm’, ‘levenberg-marquardt’, ‘MaxFunctionEvaluations’,10^5, ‘MaxIterations’, 10^5, ‘FunctionTolerance’,10^-20, ‘StepTolerance’, 10^-20);
delta_Abs(:,1)= data_new2(:,1);
x1 = lsqcurvefit(@EM_TA_wR1, x0, E_p, delta_Abs1, lb, ub, optim_lsq);
x2 = lsqcurvefit(@EM_TA_wR2, x1, E_p, delta_Abs2, lb, ub, optim_lsq);
x3 = lsqcurvefit(@EM_TA_wR3, x2, E_p, delta_Abs3, lb, ub, optim_lsq);
x4 = lsqcurvefit(@EM_TA_wR4, x3, E_p, delta_Abs4, lb, ub, optim_lsq);
plot(E_p, delta_Abs1, ‘o’,’color’,’blue’)
hold on
plot(E_p, delta_Abs2, ‘o’, ‘color’, ‘yellow’)
plot(E_p, delta_Abs3, ‘o’, ‘color’, ‘green’)
plot(E_p, delta_Abs4, ‘o’, ‘color’, ‘magenta’)
plot(E_p, EM_TA_wR1(x1, E_p), ‘color’, ‘blue’, ‘LineWidth’, 4.0)
plot(E_p, EM_TA_wR2(x2, E_p), ‘color’, ‘yellow’, ‘LineWidth’, 4.0)
plot(E_p, EM_TA_wR3(x3, E_p), ‘color’, ‘green’, ‘LineWidth’, 4.0)
plot(E_p, EM_TA_wR4(x4, E_p), ‘color’, ‘magenta’, ‘LineWidth’, 4.0)
xlabel(‘Probe Photon Energy (eV)’)
ylabel(‘Delta A (O.D.)’)
legend(‘0.5 ps’, ‘1.0 ps’, ‘2.0 ps’, ‘4.0 ps’, ‘Location’, ‘southeast’)
hold off
disp(x1)
disp(x2)
disp(x3)
disp(x4)
toc Hi all, I am working on a curve fitting project and I have a couple of curves to fit. For 2 of the curves, the fitting function involves an integration and I was told that I will need to make each of the data loop through the function (using the for loop). The below is my first code, which takes less than a second to complete.
tic
% Preparation
clear; clc
load Steady_State_Data.mat
%% Preamble
% Fundamental constants
h = 4.0135667696*10^-15; % units: eV/ Hz
c = 3*10^8; % SI units
% Clean up of data to select range of values
Absorbance = log10(T_substrate./T_sample);
e = E >= 0 & E <= 2.0; % returns boolean value of the indices that satisfies the logical condition defined above
A = find(e); % gives indices that are non-zero
N = length(A); % no. of elements that are non-zero
n = length(E) + 1;
% Data for fitting
E_p = E(n-N:n-1);
Abs = Absorbance(n-N:n-1) – min(Absorbance);
% Fitting function: Elliott’s Model for Steady State with non-parabolic factor
function F = EM_SS_wR(p, e_p)
for i = 1:numel(e_p)
E_p = e_p(i);
F(i) = p(1)*(2*pi*sqrt(p(4))/E_p)*(1/p(6))*…
(integral(@(E)sech(((E_p – E)./p(6)))*(1 + 10*p(5)*(E – p(3)) + …
126*(p(5))^2*(E – p(3))^2)/(1 – exp(-2*pi*sqrt(p(4)/(E – p(3))))), p(3), Inf, ‘ArrayValued’, 1)) + …
p(2)*((2*pi*p(4)^3/2)/E_p)*1/p(6)*(…
(1/1^3)*sech((E_p – p(3) + p(4)/1^2)./p(6)) + …
(1/2^3)*sech((E_p – p(3) + p(4)/2^2)./p(6)) + …
(1/3^3)*sech((E_p – p(3) + p(4)/3^2)./p(6)) + …
(1/4^3)*sech((E_p – p(3) + p(4)/4^2)./p(6)) + …
(1/5^3)*sech((E_p – p(3) + p(4)/5^2)./p(6)) + …
(1/6^3)*sech((E_p – p(3) + p(4)/6^2)./p(6)) + …
(1/7^3)*sech((E_p – p(3) + p(4)/7^2)./p(6)));
end
F = F(:);
end
% Carrier Contribution
function F = EM_SS_wR_CC(p, e_p)
for i = 1:numel(e_p)
E_p = e_p(i);
F(i) = p(1)*(2*pi*sqrt(p(4))/E_p)*(1/p(6))*…
(integral(@(E)sech(((E_p – E)./p(6)))*(1 + 10*p(5)*(E – p(3)) + …
126*(p(5))^2*(E – p(3))^2)/(1 – exp(-2*pi*sqrt(p(4)/(E – p(3))))), p(3), Inf, ‘ArrayValued’, 1));
end
F = F(:);
end
% Excitoninc Contribution
EM_SS_wR_EC = @(p, E_p) p(2)*((2*pi*p(4)^3/2)/E_p)*1/p(6)*(…
(1/1^3)*sech((E_p – p(3) + p(4)/1^2)./p(6)) + …
(1/2^3)*sech((E_p – p(3) + p(4)/2^2)./p(6)) + …
(1/3^3)*sech((E_p – p(3) + p(4)/3^2)./p(6)) + …
(1/4^3)*sech((E_p – p(3) + p(4)/4^2)./p(6)) + …
(1/5^3)*sech((E_p – p(3) + p(4)/5^2)./p(6)) + …
(1/6^3)*sech((E_p – p(3) + p(4)/6^2)./p(6)) + …
(1/7^3)*sech((E_p – p(3) + p(4)/7^2)./p(6)));
%% Curve fitting options
% Initial parameter guess and bounds
lb = [-Inf, -Inf, 0, 0, -Inf, -Inf]; ub = [Inf, Inf, 55, 0.030, Inf, Inf];
p0 = [0.13, 0.11, 1.4, 0.025, 0.12, 0.035]; % refer to the next line for their order
% p0 = [A1, A2, Eg, Eb, R, g]
% lsqcurvefit and choose between different algorithm that lsqcurvefit employs (3C1, comment those lines that are not choosen and uncomment the line that is choosen, if not, matlab will take the last line of "optim_lsq" by default)
optim_lsq = optimoptions(‘lsqcurvefit’, ‘Algorithm’, ‘levenberg-marquardt’, ‘MaxFunctionEvaluations’,1000, ‘MaxIterations’, 1000, ‘FunctionTolerance’,10^-9, ‘StepTolerance’, 10^-9);
% optim_lsq = optimoptions(‘lsqcurvefit’, ‘Algorithm’, ‘trust-region-reflective’, ‘MaxFunctionEvaluations’,1000, ‘MaxIterations’, ‘FunctionTolerance’,10^-9, ‘StepTolerance’, 10^-9);
% optim_lsq = optimoptions(‘lsqcurvefit’, ‘Algorithm’, ‘interior-point’, ‘MaxFunctionEvaluations’,1000, ‘MaxIterations’, 1000, ‘FunctionTolerance’,10^-9, ‘StepTolerance’, 10^-9);
% Solver for lsqcurvefit
[p, residualnorm, residual, exitflag, output, lambda, jacobian] = lsqcurvefit(@EM_SS_wR, p0, E_p, Abs, lb, ub);
%% Error bars calculation
ci = nlparci(p, residual, ‘Jacobian’, jacobian);
PCI = table(ci(:,1), p(:), ci(:,2),’VariableNames’,{‘CI 0.025′,’p’,’CI 0.975′});
Parameter_CI = table2array(PCI);
upper_bar = (Parameter_CI(:,3) – Parameter_CI(:,2))./2;
lower_bar = (Parameter_CI(:,2) – Parameter_CI(:,1))./2;
%% Parameter values (refer to command window)
p1 = p(1,1);
p2 = p(1,2);
p3 = p(1,3);
p4 = p(1,4);
p5 = p(1,5);
p6 = p(1,6);
e1 = lower_bar(1,1);
e2 = lower_bar(2,1);
e3 = lower_bar(3,1);
e4 = lower_bar(4,1);
e5 = lower_bar(5,1);
e6 = lower_bar(6,1);
X1 = [‘ A1 = ‘, num2str(p1), char(177), num2str(e1)];
X2 = [‘ A2 = ‘, num2str(p2), char(177), num2str(e2)];
X3 = [‘ Eg = ‘, num2str(p3), char(177), num2str(e3)];
X4 = [‘ Eb = ‘, num2str(p4), char(177), num2str(e4)];
X5 = [‘ R = ‘, num2str(p5), char(177), num2str(e5)];
X6 = [‘ g = ‘, num2str(p6), char(177), num2str(e6)];
disp(X1);
disp(X2);
disp(X3);
disp(X4);
disp(X5);
disp(X6);
% Table of parameter values
parameter_name = {‘A1’; ‘A2’; ‘Eg’; ‘Eb’; ‘R’; ‘g’};
parameter_values = [p1; p2; p3; p4; p5; p6];
error = [e1; e2; e3; e4; e5; e6];
T = table(parameter_values, error, ‘RowNames’,parameter_name);
disp(T)
%% Plot command
plot(E_p, Abs, ‘o’, ‘Color’,’b’) % scatter plot
hold on
plot(E_p, EM_SS_wR(p, E_p), ‘LineWidth’, 1.0, ‘Color’,’red’) % curve fitting
plot(E_p, EM_SS_wR_CC(p, E_p), ‘LineWidth’, 1.0,’Color’,’black’) % carrier contribution
plot(E_p, EM_SS_wR_EC(p, E_p), ‘LineWidth’, 1.0, ‘Color’,’green’) % excitonic contribution
% Table of parameter values
TString = evalc(‘disp(T)’);
TString = strrep(TString,'<strong>’,’bf’);
TString = strrep(TString,'</strong>’,’rm’);
TString = strrep(TString,’_’,’_’);
FixedWidth = get(0,’FixedWidthFontName’);
annotation(gcf,’Textbox’,’String’,TString,’Interpreter’,’tex’,…
‘FontName’,FixedWidth,’Units’,’Normalized’,’Position’,[0.15, 0.8, 0.1, 0.1]);
xlabel(‘Probe energy (eV)’)
ylabel(‘Absorbance.O.D’)
legend(‘Experimental Data’, ‘Fitted Curve’, ‘Carrier Contribution’,’Excitonic Contribution’, ‘Location’,’southeast’)
hold off
%% Miscellaneous
% For a rectangular pic:
% drag horizontally until it just covers the letter ‘c’ in the word ‘col’
% For a square pic:
% drag horizontall until it just covers the letter ‘o’ in the word ‘Contribution’ in the next line)
% legend(‘Experimental Data’, ‘Fitted Curve’, ‘Carrier Contribution’,’Excitonic Contribution’)
toc
However, when using my second code (shown below) it takes around 5 minutes to complete running. May I please ask for the difference in the computational time?
tic
%% Preparation
clc; clear
data = importdata("FCPIB-293K-2.5mW-400nm-Jan072021 -ibg -bg -chirp.csv"); % insert file path within parenthesis
load Steady_State_Parameter_Values.mat
%% 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 = data(:, 1);% units: nm
E = (h*c)./(Wavelength*10^-9);
delay_t = data(1, :);
% Parameter values from Steady state fiting
A1 = p(1,1);
A2 = p(1,2);
Eg = p(1,3);
Eb = p(1,4);
R = p(1,5);
g = p(1,6);
t1 = 0.5342;
t2 = 1.0257;
t3 = 1.9673;
t4 = 3.9554;
carrier_T = [1198.8, 816.7, 446.8, 328.7];
col1 = 56;
col2 = 63;
col3 = 74;
col4 = 87;
% Data for fitting
Range_E = E >= 1.5 & E <= 2.0;
Range_W = Wavelength >= (h*c)/(2.0*10^-9) & Wavelength <= (h*c)/(1.5*10^-9);
E_p = E(Range_E); % selected probe energies
data_new2 = data(Range_W, [col1,col2,col3,col4]);
delta_Abs1 = data(Range_W,col1);
delta_Abs2 = data(Range_W,col2);
delta_Abs3 = data(Range_W,col3);
delta_Abs4 = data(Range_W,col4);
% Fitting function: Elliott’s Model for Transient Absorption
function F = EM_TA_wR1(x, e_p)
load Steady_State_Parameter_Values.mat
kB = 8.617333268*10^-5; % units: eV/ K
A1 = p(1,1);
A2 = p(1,2);
Eg = p(1,3);
Eb = p(1,4);
R = p(1,5);
g = p(1,6);
carrier_T = [1198.8, 816.7, 446.8, 328.7];
n = 1;
for i = 1:numel(e_p)
E_p = e_p(i);
F(i) = x(5)*A1*((2*pi*sqrt(x(2)))/E_p)*1/g*(integral(@(E)sech(((E_p – E)./g))*(1 + 10*R*(E – x(1)) + …
126*(R)^2*(E – x(1))^2)/(1 – exp(-2*pi*sqrt(x(2)/(E – x(1))))), x(1), Inf, ‘ArrayValued’, 1))*(1 – 1/(1 + exp((E_p – x(4))/(kB*carrier_T(1, n)))))^2 – …
A1*(2*pi*sqrt(Eb)/E_p)*(1/g)*…
(integral(@(e)sech(((E_p – e)./g))*(1 + 10*R*(e – Eg) + …
126*(R)^2*(e – Eg)^2)/(1 – exp(-2*pi*sqrt(Eb/(e – Eg)))), Eg, Inf, ‘ArrayValued’, 1)) + …
x(6)*A2*(4*pi*(x(2))^3/2)./(E_p).*1/x(3)*(…
(1/1^3)*sech((E_p – x(1) + x(2)/1^2)./x(3)) + …
(1/2^3)*sech((E_p – x(1) + x(2)/2^2)./x(3)) + …
(1/3^3)*sech((E_p – x(1) + x(2)/3^2)./x(3)) + …
(1/4^3)*sech((E_p – x(1) + x(2)/4^2)./x(3)) + …
(1/5^3)*sech((E_p – x(1) + x(2)/5^2)./x(3)) + …
(1/6^3)*sech((E_p – x(1) + x(2)/6^2)./x(3)) + …
(1/7^3)*sech((E_p – x(1) + x(2)/7^2)./x(3))) – …
A2*(4*pi*Eb^3/2)./E_p.*1/g*(…
(1/1^3)*sech((E_p – Eg + Eb/1^2)./g) + …
(1/2^3)*sech((E_p – Eg + Eb/2^2)./g) + …
(1/3^3)*sech((E_p – Eg + Eb/3^2)./g) + …
(1/4^3)*sech((E_p – Eg + Eb/4^2)./g) + …
(1/5^3)*sech((E_p – Eg + Eb/5^2)./g) + …
(1/6^3)*sech((E_p – Eg + Eb/6^2)./g) + …
(1/7^3)*sech((E_p – Eg + Eb/7^2)./g));
end
F = F(:);
end
function F = EM_TA_wR2(x, e_p)
data = importdata("Experimental dataTransient AbsorptionFCPIB-293K-2.5mW-400nm-Jan072021 -ibg -bg -chirp.csv"); % insert file path within parenthesis
load Steady_State_Parameter_Values.mat
h = 4.1356677*10^-15; % units: eV/ Hz
c = 3*10^8; % SI units
kB = 8.617333268*10^-5; % units: eV/ K
Wavelength = data(:, 1);% units: nm
E = (h*c)./(Wavelength*10^-9);
A1 = p(1,1);
A2 = p(1,2);
Eg = p(1,3);
Eb = p(1,4);
R = p(1,5);
g = p(1,6);
col1 = 56;
col2 = 63;
col3 = 74;
col4 = 87;
Range_E = E >= 1.5 & E <= 2.0;
Range_W = Wavelength >= (h*c)/(2.0*10^-9) & Wavelength <= (h*c)/(1.5*10^-9);
E_p = E(Range_E); % selected probe energies
data_new2 = data(Range_W, [col1,col2,col3,col4]);
carrier_T = [1198.8, 816.7, 446.8, 328.7];
n = 2;
for i = 1:numel(e_p)
E_p = e_p(i);
F(i) = x(5)*A1*((2*pi*sqrt(x(2)))/E_p)*1/g*(integral(@(E)sech(((E_p – E)./g))*(1 + 10*R*(E – x(1)) + …
126*(R)^2*(E – x(1))^2)/(1 – exp(-2*pi*sqrt(x(2)/(E – x(1))))), x(1), Inf, ‘ArrayValued’, 1))*(1 – 1/(1 + exp((E_p – x(4))/(kB*carrier_T(1, n)))))^2 – …
A1*(2*pi*sqrt(Eb)/E_p)*(1/g)*…
(integral(@(e)sech(((E_p – e)./g))*(1 + 10*R*(e – Eg) + …
126*(R)^2*(e – Eg)^2)/(1 – exp(-2*pi*sqrt(Eb/(e – Eg)))), Eg, Inf, ‘ArrayValued’, 1)) + …
x(6)*A2*(4*pi*(x(2))^3/2)*1/x(3)*(…
(1/1^3)*sech((E_p – x(1) + x(2)/1^2)./x(3)) + …
(1/2^3)*sech((E_p – x(1) + x(2)/2^2)./x(3)) + …
(1/3^3)*sech((E_p – x(1) + x(2)/3^2)./x(3)) + …
(1/4^3)*sech((E_p – x(1) + x(2)/4^2)./x(3)) + …
(1/5^3)*sech((E_p – x(1) + x(2)/5^2)./x(3)) + …
(1/6^3)*sech((E_p – x(1) + x(2)/6^2)./x(3)) + …
(1/7^3)*sech((E_p – x(1) + x(2)/7^2)./x(3))) – …
A2*(4*pi*Eb^3/2)*1/g*(…
(1/1^3)*sech((E_p – Eg + Eb/1^2)./g) + …
(1/2^3)*sech((E_p – Eg + Eb/2^2)./g) + …
(1/3^3)*sech((E_p – Eg + Eb/3^2)./g) + …
(1/4^3)*sech((E_p – Eg + Eb/4^2)./g) + …
(1/5^3)*sech((E_p – Eg + Eb/5^2)./g) + …
(1/6^3)*sech((E_p – Eg + Eb/6^2)./g) + …
(1/7^3)*sech((E_p – Eg + Eb/7^2)./g));
end
F = F(:);
end
function F = EM_TA_wR3(x, e_p)
load Steady_State_Parameter_Values.mat
h = 4.1356677*10^-15; % units: eV/ Hz
c = 3*10^8; % SI units
kB = 8.617333268*10^-5; % units: eV/ K
Wavelength = data(:, 1);% units: nm
E = (h*c)./(Wavelength*10^-9);
A1 = p(1,1);
A2 = p(1,2);
Eg = p(1,3);
Eb = p(1,4);
R = p(1,5);
g = p(1,6);
col1 = 56;
col2 = 63;
col3 = 74;
col4 = 87;
Range_E = E >= 1.5 & E <= 2.0;
Range_W = Wavelength >= (h*c)/(2.0*10^-9) & Wavelength <= (h*c)/(1.5*10^-9);
E_p = E(Range_E); % selected probe energies
data_new2 = data(Range_W, [col1,col2,col3,col4]);
carrier_T = [1198.8, 816.7, 446.8, 328.7];
n = 3;
for i = 1:numel(e_p)
E_p = e_p(i);
F(i) = x(5)*A1*((2*pi*sqrt(x(2)))/E_p)*1/g*(integral(@(E)sech(((E_p – E)./g))*(1 + 10*R*(E – x(1)) + …
126*(R)^2*(E – x(1))^2)/(1 – exp(-2*pi*sqrt(x(2)/(E – x(1))))), x(1), Inf, ‘ArrayValued’, 1))*(1 – 1/(1 + exp((E_p – x(4))/(kB*carrier_T(1, n)))))^2 – …
A1*(2*pi*sqrt(Eb)/E_p)*(1/g)*…
(integral(@(e)sech(((E_p – e)./g))*(1 + 10*R*(e – Eg) + …
126*(R)^2*(e – Eg)^2)/(1 – exp(-2*pi*sqrt(Eb/(e – Eg)))), Eg, Inf, ‘ArrayValued’, 1)) + …
x(6)*A2*(4*pi*(x(2))^3/2)*1/x(3)*(…
(1/1^3)*sech((E_p – x(1) + x(2)/1^2)./x(3)) + …
(1/2^3)*sech((E_p – x(1) + x(2)/2^2)./x(3)) + …
(1/3^3)*sech((E_p – x(1) + x(2)/3^2)./x(3)) + …
(1/4^3)*sech((E_p – x(1) + x(2)/4^2)./x(3)) + …
(1/5^3)*sech((E_p – x(1) + x(2)/5^2)./x(3)) + …
(1/6^3)*sech((E_p – x(1) + x(2)/6^2)./x(3)) + …
(1/7^3)*sech((E_p – x(1) + x(2)/7^2)./x(3))) – …
A2*(4*pi*Eb^3/2)*1/g*(…
(1/1^3)*sech((E_p – Eg + Eb/1^2)./g) + …
(1/2^3)*sech((E_p – Eg + Eb/2^2)./g) + …
(1/3^3)*sech((E_p – Eg + Eb/3^2)./g) + …
(1/4^3)*sech((E_p – Eg + Eb/4^2)./g) + …
(1/5^3)*sech((E_p – Eg + Eb/5^2)./g) + …
(1/6^3)*sech((E_p – Eg + Eb/6^2)./g) + …
(1/7^3)*sech((E_p – Eg + Eb/7^2)./g));
end
F = F(:);
end
function F = EM_TA_wR4(x, e_p)
load Steady_State_Parameter_Values.mat
h = 4.1356677*10^-15; % units: eV/ Hz
c = 3*10^8; % SI units
kB = 8.617333268*10^-5; % units: eV/ K
Wavelength = data(:, 1);% units: nm
E = (h*c)./(Wavelength*10^-9);
A1 = p(1,1);
A2 = p(1,2);
Eg = p(1,3);
Eb = p(1,4);
R = p(1,5);
g = p(1,6);
col1 = 56;
col2 = 63;
col3 = 74;
col4 = 87;
Range_E = E >= 1.5 & E <= 2.0;
Range_W = Wavelength >= (h*c)/(2.0*10^-9) & Wavelength <= (h*c)/(1.5*10^-9);
E_p = E(Range_E); % selected probe energies
data_new2 = data(Range_W, [col1,col2,col3,col4]);
carrier_T = [1198.8, 816.7, 446.8, 328.7];
n = 4;
for i = 1:numel(e_p)
E_p = e_p(i);
F(i) = x(5)*A1*((2*pi*sqrt(x(2)))/E_p)*1/g*(integral(@(E)sech(((E_p – E)./g))*(1 + 10*R*(E – x(1)) + …
126*(R)^2*(E – x(1))^2)/(1 – exp(-2*pi*sqrt(x(2)/(E – x(1))))), x(1), Inf, ‘ArrayValued’, 1))*(1 – 1/(1 + exp((E_p – x(4))/(kB*carrier_T(1, n)))))^2 – …
A1*(2*pi*sqrt(Eb)/E_p)*(1/g)*…
(integral(@(e)sech(((E_p – e)./g))*(1 + 10*R*(e – Eg) + …
126*(R)^2*(e – Eg)^2)/(1 – exp(-2*pi*sqrt(Eb/(e – Eg)))), Eg, Inf, ‘ArrayValued’, 1)) + …
x(6)*A2*(4*pi*(x(2))^3/2)*1/x(3)*(…
(1/1^3)*sech((E_p – x(1) + x(2)/1^2)./x(3)) + …
(1/2^3)*sech((E_p – x(1) + x(2)/2^2)./x(3)) + …
(1/3^3)*sech((E_p – x(1) + x(2)/3^2)./x(3)) + …
(1/4^3)*sech((E_p – x(1) + x(2)/4^2)./x(3)) + …
(1/5^3)*sech((E_p – x(1) + x(2)/5^2)./x(3)) + …
(1/6^3)*sech((E_p – x(1) + x(2)/6^2)./x(3)) + …
(1/7^3)*sech((E_p – x(1) + x(2)/7^2)./x(3))) – …
A2*(4*pi*Eb^3/2)*1/g*(…
(1/1^3)*sech((E_p – Eg + Eb/1^2)./g) + …
(1/2^3)*sech((E_p – Eg + Eb/2^2)./g) + …
(1/3^3)*sech((E_p – Eg + Eb/3^2)./g) + …
(1/4^3)*sech((E_p – Eg + Eb/4^2)./g) + …
(1/5^3)*sech((E_p – Eg + Eb/5^2)./g) + …
(1/6^3)*sech((E_p – Eg + Eb/6^2)./g) + …
(1/7^3)*sech((E_p – Eg + Eb/7^2)./g));
end
F = F(:);
end
% Curve fitting
lb = [Eg, Eb, g, 0.3, 0.5, 0.2]; ub = [55, 0.05, 0.05, 20, 2, 1];
x0 = [1.65, 0.03, 0.03, 1.3, 1, 0.3];
optim_lsq = optimoptions(‘lsqcurvefit’, ‘Algorithm’, ‘levenberg-marquardt’, ‘MaxFunctionEvaluations’,10^5, ‘MaxIterations’, 10^5, ‘FunctionTolerance’,10^-20, ‘StepTolerance’, 10^-20);
delta_Abs(:,1)= data_new2(:,1);
x1 = lsqcurvefit(@EM_TA_wR1, x0, E_p, delta_Abs1, lb, ub, optim_lsq);
x2 = lsqcurvefit(@EM_TA_wR2, x1, E_p, delta_Abs2, lb, ub, optim_lsq);
x3 = lsqcurvefit(@EM_TA_wR3, x2, E_p, delta_Abs3, lb, ub, optim_lsq);
x4 = lsqcurvefit(@EM_TA_wR4, x3, E_p, delta_Abs4, lb, ub, optim_lsq);
plot(E_p, delta_Abs1, ‘o’,’color’,’blue’)
hold on
plot(E_p, delta_Abs2, ‘o’, ‘color’, ‘yellow’)
plot(E_p, delta_Abs3, ‘o’, ‘color’, ‘green’)
plot(E_p, delta_Abs4, ‘o’, ‘color’, ‘magenta’)
plot(E_p, EM_TA_wR1(x1, E_p), ‘color’, ‘blue’, ‘LineWidth’, 4.0)
plot(E_p, EM_TA_wR2(x2, E_p), ‘color’, ‘yellow’, ‘LineWidth’, 4.0)
plot(E_p, EM_TA_wR3(x3, E_p), ‘color’, ‘green’, ‘LineWidth’, 4.0)
plot(E_p, EM_TA_wR4(x4, E_p), ‘color’, ‘magenta’, ‘LineWidth’, 4.0)
xlabel(‘Probe Photon Energy (eV)’)
ylabel(‘Delta A (O.D.)’)
legend(‘0.5 ps’, ‘1.0 ps’, ‘2.0 ps’, ‘4.0 ps’, ‘Location’, ‘southeast’)
hold off
disp(x1)
disp(x2)
disp(x3)
disp(x4)
toc curve fitting, computational time MATLAB Answers — New Questions
How can i run my .m file of Report Generator?
Hi, I wanted to know how to use the .m file from my report that I customized and saved with the "Generate MATLAB File" option, because I need to use that .m file in a MATLAB function. But when I run the .m file, it doesn’t generate any report. It only shows this:
What can i do?
Thanks
How i generate the .m:Hi, I wanted to know how to use the .m file from my report that I customized and saved with the "Generate MATLAB File" option, because I need to use that .m file in a MATLAB function. But when I run the .m file, it doesn’t generate any report. It only shows this:
What can i do?
Thanks
How i generate the .m: Hi, I wanted to know how to use the .m file from my report that I customized and saved with the "Generate MATLAB File" option, because I need to use that .m file in a MATLAB function. But when I run the .m file, it doesn’t generate any report. It only shows this:
What can i do?
Thanks
How i generate the .m: "report-generator" "matlab" MATLAB Answers — New Questions
How can I disable data logging for all the signals in my model?
I have enabled data logging for a lot of signals in my model, how can do this via command line functions?I have enabled data logging for a lot of signals in my model, how can do this via command line functions? I have enabled data logging for a lot of signals in my model, how can do this via command line functions? MATLAB Answers — New Questions
In matlab, although my matrix is full rank, the solution is still incorrect.Ax=b,A(10000*10000),x、b(10000*1)
This is equation solving
this is D matrics
this is J matrics
I want to test
but the result is not good
My ultimate goal is to use the constructed inverse matrix to restore the image, because the deviation in the solution causes the restoration effect not to be very good:
The above picture is restored by me directly using the inverse matrix. The picture below is restored by using the constructed inverse matrix. The inverse matrix is J and the constructed inverse matrix is Dx1.
Thank you guysThis is equation solving
this is D matrics
this is J matrics
I want to test
but the result is not good
My ultimate goal is to use the constructed inverse matrix to restore the image, because the deviation in the solution causes the restoration effect not to be very good:
The above picture is restored by me directly using the inverse matrix. The picture below is restored by using the constructed inverse matrix. The inverse matrix is J and the constructed inverse matrix is Dx1.
Thank you guys This is equation solving
this is D matrics
this is J matrics
I want to test
but the result is not good
My ultimate goal is to use the constructed inverse matrix to restore the image, because the deviation in the solution causes the restoration effect not to be very good:
The above picture is restored by me directly using the inverse matrix. The picture below is restored by using the constructed inverse matrix. The inverse matrix is J and the constructed inverse matrix is Dx1.
Thank you guys matrics, calculation MATLAB Answers — New Questions
Incorrect dimensions for matrix multiplication.
Could anyone help, please with explanation?
V=1+((exp(-0.5*t))*((-0.9)*(cos(1.323*t))-(0.34)*(sin(1.323*t))));
Error using *
Incorrect dimensions for matrix multiplication. Check that the number
of columns in the first matrix matches the number of rows in the
second matrix. To perform elementwise multiplication, use ‘.*’.
Thank you in advance.Could anyone help, please with explanation?
V=1+((exp(-0.5*t))*((-0.9)*(cos(1.323*t))-(0.34)*(sin(1.323*t))));
Error using *
Incorrect dimensions for matrix multiplication. Check that the number
of columns in the first matrix matches the number of rows in the
second matrix. To perform elementwise multiplication, use ‘.*’.
Thank you in advance. Could anyone help, please with explanation?
V=1+((exp(-0.5*t))*((-0.9)*(cos(1.323*t))-(0.34)*(sin(1.323*t))));
Error using *
Incorrect dimensions for matrix multiplication. Check that the number
of columns in the first matrix matches the number of rows in the
second matrix. To perform elementwise multiplication, use ‘.*’.
Thank you in advance. matlab MATLAB Answers — New Questions
how to build one to many and many to many LSTM in matlab?
I don’t know that is possible to build these kind of networks in matlab? If it is possible, can I use these kind of network in simulink?I don’t know that is possible to build these kind of networks in matlab? If it is possible, can I use these kind of network in simulink? I don’t know that is possible to build these kind of networks in matlab? If it is possible, can I use these kind of network in simulink? lstm, one_to_many, many_to_many, recurrent_neural_networks, neural network MATLAB Answers — New Questions
How do I stitch these data and get a final plot
https://drive.google.com/file/d/10Eum5JdntahZQYObOIt2f8HLHd8XgCj1/view?usp=drive_link
this is my code
https://drive.google.com/file/d/16knhWQfi8oZHHg2GBlMJKW-hlOGuYgDO/view?usp=drive_link
https://drive.google.com/file/d/1b6FtjMr7bINlB6UEetemgwzKJjI7KPM9/view?usp=drive_link
I want to take all the 61*6 X data(1st, 4th,7th…columns) , all 61*6 Y data (2nd, 5th,8th…colums) and all 61*6 Z data(3rd,6th,9th …columns) stitch all of them and get the final surface plot of X, Y, Z. How do I do this? Can anybody please help sooner.https://drive.google.com/file/d/10Eum5JdntahZQYObOIt2f8HLHd8XgCj1/view?usp=drive_link
this is my code
https://drive.google.com/file/d/16knhWQfi8oZHHg2GBlMJKW-hlOGuYgDO/view?usp=drive_link
https://drive.google.com/file/d/1b6FtjMr7bINlB6UEetemgwzKJjI7KPM9/view?usp=drive_link
I want to take all the 61*6 X data(1st, 4th,7th…columns) , all 61*6 Y data (2nd, 5th,8th…colums) and all 61*6 Z data(3rd,6th,9th …columns) stitch all of them and get the final surface plot of X, Y, Z. How do I do this? Can anybody please help sooner. https://drive.google.com/file/d/10Eum5JdntahZQYObOIt2f8HLHd8XgCj1/view?usp=drive_link
this is my code
https://drive.google.com/file/d/16knhWQfi8oZHHg2GBlMJKW-hlOGuYgDO/view?usp=drive_link
https://drive.google.com/file/d/1b6FtjMr7bINlB6UEetemgwzKJjI7KPM9/view?usp=drive_link
I want to take all the 61*6 X data(1st, 4th,7th…columns) , all 61*6 Y data (2nd, 5th,8th…colums) and all 61*6 Z data(3rd,6th,9th …columns) stitch all of them and get the final surface plot of X, Y, Z. How do I do this? Can anybody please help sooner. stitchdata, data MATLAB Answers — New Questions
MATLAB error when PC idle: render process terminated: null
I am getting following error when I kept MATLAB idle for long hours (No codes running, overnight). I am using 2021a version (academic license). I am not sure this error is interfering anywhere while runnning codes or simulink.
render process terminated: null
java.lang.NoSuchFieldError: TS_PROCESS_OOMI am getting following error when I kept MATLAB idle for long hours (No codes running, overnight). I am using 2021a version (academic license). I am not sure this error is interfering anywhere while runnning codes or simulink.
render process terminated: null
java.lang.NoSuchFieldError: TS_PROCESS_OOM I am getting following error when I kept MATLAB idle for long hours (No codes running, overnight). I am using 2021a version (academic license). I am not sure this error is interfering anywhere while runnning codes or simulink.
render process terminated: null
java.lang.NoSuchFieldError: TS_PROCESS_OOM java, matlab MATLAB Answers — New Questions
Versions of Matlab suppoting Robotic Systems toolbox.
Hello, I wanted to know if the 2014 version of MATLAB-Simulink supports the Robotic Systems toolbox. IHello, I wanted to know if the 2014 version of MATLAB-Simulink supports the Robotic Systems toolbox. I Hello, I wanted to know if the 2014 version of MATLAB-Simulink supports the Robotic Systems toolbox. I robotic systems toolbox, versions MATLAB Answers — New Questions