Filter löschen
Filter löschen

Making a global variable for the pdepe solver

2 Ansichten (letzte 30 Tage)
Shuaib Balogun
Shuaib Balogun am 11 Feb. 2022
Bearbeitet: Walter Roberson am 19 Okt. 2023
In the pdepe solver, it follows the form :
m = 1;
sol = pdepe(m,@heatcyl,@heatic,@heatbc,x,t);
where @heatcyl,@heatic,@heatbc are functions that are defined.
In the following function, I would like to make the value n in this function something that I can change globally.
function u0 = heatic(x)
n = 2.404825557695773;
u0 = besselj(0,n*x);
end
My goal is to create a python program that calls the solver pdepe and inputs variables such as n. I have found for all the documentation online that you have to change the variable in each function each time, instead of just declaring it in - sol = pdepe(m,@heatcyl,@heatic,@heatbc,x,t) or globally. I would like to scale up the program to be able to solve different equations with different values of variables as I input it but I do not quite understand how to go about it
Additionally, how would someone call this function "sol = pdepe(m,@heatcyl,@heatic,@heatbc,x,t)" in the matlab api in python?

Antworten (1)

Infinite_king
Infinite_king am 19 Okt. 2023
Hi Shuaib Balogun,
I understand that you want to change the variables values within functions without editing the function code. Specifically, you want the updated value to be reflected in all functions. Finally, you want to call the MATLAB functions from python.
You can use global variables to change the variable values in all functions. To know how to declare and use global variables please refer the below code,
% declaring n as a global variable
global n;
% setting 'n' values as 10
n = 10;
fun1('some input')
value of 'n' in function 1 is 10
fun2('some input')
value of 'n' in function 2 is 10
% changing the 'n' value
n = 20;
fun1('some input')
value of 'n' in function 1 is 20
fun2('some input')
value of 'n' in function 2 is 20
function ret = fun1(inp)
% declaring that 'n' is a global variable
global n;
% From this point global value of n is used
disp("value of 'n' in function 1 is " + n);
end
function ret = fun2(inp)
% declaring that 'n' is a global variable
global n;
% From this point global value of n is used
disp("value of 'n' in function 2 is " + n);
end
MATLAB functions and user scripts can be called from Python using ‘MATLAB Engine API for Python’. Install the Engine API as per the instructions in the following MATLAB documentation - https://www.mathworks.com/help/matlab/matlab_external/install-the-matlab-engine-for-python.html
Please note that you must select the correct version that is compatible with your MATLAB version in the following release page - https://pypi.org/project/matlabengine/9.14.2/#history
Once MATLAB engine setup is completed, MATLAB functions can be used from python as follows,
import matlab.engine
# Start the MATLAB engine
eng = matlab.engine.start_matlab()
# Declaring 'n' as a global variable
eng.eval('global n', nargout=0)
# Set the value of n in MATLAB
eng.eval('n = 10', nargout=0)
# Define other necessary variables (e.g., m, x, t, and functions)
# Call the pdepe solver in MATLAB
sol = eng.pdepe(m, 'heatcyl', 'heatic', 'heatbc', x, t)
# Close the MATLAB engine
eng.quit()
For more information, please refer the following MATLAB documentation,
  1. https://www.mathworks.com/help/matlab/ref/global.html
  2. https://www.mathworks.com/help/matlab/ref/pdepe.html#d126e1166404
  3. https://www.mathworks.com/help/matlab/matlab_external/call-user-script-and-function-from-python.html
Hope this is helpful.

Kategorien

Mehr zu Call MATLAB from Python finden Sie in Help Center und File Exchange

Produkte


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by