modify .m file using a .m file

I have a .m file called ilap.m and I need to change one line of this code inside of another .m file, called main.m. I know that similar questions have been asked before, but I have no experience doing something like this, so I do not understand these explanations. Therefore, it would be nice if someone could show me an easy example so that I could understand how to perform this task.
Thanks in advance!

2 Comments

What changes you want to make? How the .m file is?
Wout Laeremans
Wout Laeremans on 12 Oct 2022
Edited: Wout Laeremans on 12 Oct 2022
The m. file has the following form:
function F = ilap(p)
a = 1;
k = 11*3.9/0.44^2;
k_plus = 5;
k_on = k_plus;
k_onstar = k_on/a^2;
w = 20*a;
D_f = a^2*k;
k_off = 0.5;
F_eq = k_off./(k_onstar + k_off);
C_eq = k_onstar./(k_onstar + k_off);
F = 1./p - F_eq./p.*(1 - 2.*besselk(1,sqrt(p./D_f.*(1 + k_onstar./(p+k_off))).*w).*besseli(1,sqrt(p./D_f.*(1 + k_onstar./(p+k_off))).*w)).*(1+k_onstar./(p+k_off)) - C_eq./(p+k_off);
This m.file is used as an input for main.m, but within main.m, I want to do a forloop over 'k_off', so I need to be able to change the value of 'k_off' in main.m while running it.

Sign in to comment.

Answers (1)

So you make k_off as an input to the function and call it in a loop.
% Main function
k_off = 0:0.1:1 ;
m = length(k_off) ;
F = zeros(m,1) ;
p = rand ;
for i = 1:m
F(i) = ilap(p,k_off(i)) ;
end
plot(k_off,F)
function F = ilap(p,k_off)
a = 1;
k = 11*3.9/0.44^2;
k_plus = 5;
k_on = k_plus;
k_onstar = k_on/a^2;
w = 20*a;
D_f = a^2*k;
% k_off = 0.5; % This is made as input
F_eq = k_off./(k_onstar + k_off);
C_eq = k_onstar./(k_onstar + k_off);
F = 1./p - F_eq./p.*(1 - 2.*besselk(1,sqrt(p./D_f.*(1 + k_onstar./(p+k_off))).*w).*besseli(1,sqrt(p./D_f.*(1 + k_onstar./(p+k_off))).*w)).*(1+k_onstar./(p+k_off)) - C_eq./(p+k_off);
end

1 Comment

Hi, thanks for your answer, but I need ilap.m to only have 'p' as a variable. This is becaus ilap.m defines in inverse laplace transform and in main.m, I am making use in invlap.m [Hollenbeck, K. J. (1998) INVLAP.M: A matlab function for numerical inversion of Laplace transforms by the de Hoog algorithm, http://www.isva.dtu.dk/staff/karl/invlap.htm] to invert it, which requires an input function with one variable only.

Sign in to comment.

Categories

Find more on MATLAB in Help Center and File Exchange

Products

Release

R2021a

Tags

Asked:

on 12 Oct 2022

Commented:

on 12 Oct 2022

Community Treasure Hunt

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

Start Hunting!