Category: Matlab
Category Archives: Matlab
Error using pdepe: Spatial discretization has failed. Discretization supports only parabolic and elliptic equations, with flux term involving spatial derivative
I get this error trying to solve a system of PDEs, and I do not know if such system is solvable with ‘pdepe’. The equations are:
energy balance equation, and:
mass balance equations. Initial conditions:
Boundary conditions:
The variables in which I am solving are however T,, knowing that:
, I can write initial condition for , and I have also a value for .
I tried writing the equations in the form required by ‘pdepe’. Also, note that many parameters depends on the vector , IN particular, the term ‘v’ includes a partial spatial derivative w.r.t. P, which i transformed in a spatial derivative w.r.t. u(1) and u(2) with the above formula. Note that many constants are imported with the file.m including the initial conditions that are: This is the code of the 3 functions used inside the pde call (sorry is a bit long and messy):
function [c,f,s] = pdefun(x,t,u,dudx)
% load data
run(‘HM_data.m’);
% compute epsilon , F and dfdt
F = (hm.rho_s_in-u(3))/(u(3)*hm.tau_p – hm.rho_s_in*hm.w_max);
epsilon = 1 – (1 – hm.epsilon_0)*((1 + hm.tau_p*F)/(1 + hm.tau_a*F));
P = u(2)*u(1)*hm.R_gas/hm.MH2;
P_eq_a = (hm.C0_a*hm.C1_a*(F)^(hm.C2_a)/(1+hm.C1_a*(F)^(hm.C2_a)) + hm.C3_a*F + exp(hm.C4_a*(F-hm.C5_a)))*exp(-hm.K_a*((1/u(1))-(1/303)));
k_a = (hm.kappa_a/(1 – epsilon)) * exp(-hm.E_a/(hm.R_gas*u(1))) * log(P/P_eq_a);
dfdt = k_a*(1-F);
% compute diagonal matrix c
Cps = 6000*(3.1*hm.R + 10.04*hm.x_max*F)/(hm.Ms + 6*hm.x_max*F);
rho_C_eff = epsilon*u(2)*hm.Cpg + (1- epsilon)*u(3)*Cps;
c = [rho_C_eff; epsilon; 1-epsilon];
% COMPUTE VECTOR f
% velocity
Dp = hm.D_in*(1+ F*hm.tau_p)^(1/3);
Kp = (Dp^2)*(epsilon^3)/(150*(1-epsilon)^2);
v_cost = -Kp*hm.R_gas/(hm.mu_g*hm.MH2);
v = v_cost*(dudx(1)*u(2) + dudx(2)*u(1));
% effective thermal conductivity
N = 3.08/epsilon – 1.13;
Fn = 4/3 * hm.E_prime * hm.R^(1/2) * hm.dv^(1.5);
Hv = hm.c1*hm.dv^(hm.c2);
Rs = 0.565*Hv*hm.dv/(hm.ks*Fn);
a_H = (0.75*Fn*hm.R/hm.E_prime)^(1/3);
a_LH = [];
if epsilon <= 0.47 && epsilon >= 0.01
a_LH = 1.605/sqrt(epsilon);
elseif epsilon > 0.47 && epsilon <= 1
a_LH = 3.51 – 2.51*epsilon;
else
error(‘error epsilon’);
end
a_L = a_LH*a_H;
R_L = 1/(2*hm.ks*a_L);
Pmax = 2/pi*hm.E_prime*(hm.dv/hm.R)^(0.5);
Hc = hm.c1*(1.62*hm.dv)^(hm.c2);
a1 = erfcinv(2*Pmax/Hc);
a2 = erfcinv(0.03*Pmax/Hc) – a1;
coef_a = 2*hm.b/Dp;
coef_c = -(6*(hm.gamma-1)/(9*hm.gamma-5))* (hm.kg_ref*hm.MH2/(u(1)*u(2)*hm.R_gas))*(hm.MH2*u(1)/(2*hm.kb))^(0.5);
l_m = (-1 + sqrt(1 – 4*coef_a*coef_c))/(2*coef_a);
kg = hm.kg_ref/(1+2*hm.b*l_m/Dp);
M = (((2-hm.alpha_T1)/hm.alpha_T1)+((2-hm.alpha_T2)/hm.alpha_T2))*(2*hm.gamma/(1+hm.gamma))*(1/hm.Pr)*l_m;
R_g = sqrt(2)*hm.sigma*a2/(pi * kg * a_L^2 * log(1+ a2/(a1+M/(sqrt(2)*hm.sigma))));
L = (hm.gamma+1)*3*Dp/((9*hm.gamma-5)*4*l_m*sqrt(pi));
R_G = 1/(2*pi*kg*Dp*(0.5*log(1+L) + log(1+sqrt(L)) + 1/(1+sqrt(L)) – 1));
R_mic = Rs*R_g/(Rs+R_g);
R_c_inv = 1/(R_mic+R_L) + 1/R_G;
k_eff = N*(1-epsilon)*R_c_inv/(pi*Dp);
f = [k_eff*dudx(1); -u(2)*v; 0];
% compute vector s
m_dot_a = (1-epsilon)*(hm.rho_sat-u(3))*dfdt;
s = [u(2)*hm.Cpg*v*dudx(1) + m_dot_a*hm.delta_H ; -m_dot_a+hm.phi_abs; m_dot_a];
end
function u0 = icfun(x)
run(‘HM_data.m’);
u0 = hm.ic;
end
function [pl,ql,pr,qr] = bcfun(xl, ul, xr, ur, t)
run(‘HM_data.m’);
Nu_abs = 0.3+((0.62*(hm.Re_a^(0.5))*(hm.Pr_a^(1/3)))/(1+(0.4/hm.Pr_a)^(2/3))^(1/4)*((1+(hm.Re_a/282000)^(5/8))^(4/5)));
h_f = Nu_abs*hm.kf/hm.D_tank;
pl = [0 ; 0; 0];
ql = [1; 0; 0];
pr = [h_f*(ur(1)-hm.Tf_a); 0; 0];
qr = [-1; 0; 0];
endI get this error trying to solve a system of PDEs, and I do not know if such system is solvable with ‘pdepe’. The equations are:
energy balance equation, and:
mass balance equations. Initial conditions:
Boundary conditions:
The variables in which I am solving are however T,, knowing that:
, I can write initial condition for , and I have also a value for .
I tried writing the equations in the form required by ‘pdepe’. Also, note that many parameters depends on the vector , IN particular, the term ‘v’ includes a partial spatial derivative w.r.t. P, which i transformed in a spatial derivative w.r.t. u(1) and u(2) with the above formula. Note that many constants are imported with the file.m including the initial conditions that are: This is the code of the 3 functions used inside the pde call (sorry is a bit long and messy):
function [c,f,s] = pdefun(x,t,u,dudx)
% load data
run(‘HM_data.m’);
% compute epsilon , F and dfdt
F = (hm.rho_s_in-u(3))/(u(3)*hm.tau_p – hm.rho_s_in*hm.w_max);
epsilon = 1 – (1 – hm.epsilon_0)*((1 + hm.tau_p*F)/(1 + hm.tau_a*F));
P = u(2)*u(1)*hm.R_gas/hm.MH2;
P_eq_a = (hm.C0_a*hm.C1_a*(F)^(hm.C2_a)/(1+hm.C1_a*(F)^(hm.C2_a)) + hm.C3_a*F + exp(hm.C4_a*(F-hm.C5_a)))*exp(-hm.K_a*((1/u(1))-(1/303)));
k_a = (hm.kappa_a/(1 – epsilon)) * exp(-hm.E_a/(hm.R_gas*u(1))) * log(P/P_eq_a);
dfdt = k_a*(1-F);
% compute diagonal matrix c
Cps = 6000*(3.1*hm.R + 10.04*hm.x_max*F)/(hm.Ms + 6*hm.x_max*F);
rho_C_eff = epsilon*u(2)*hm.Cpg + (1- epsilon)*u(3)*Cps;
c = [rho_C_eff; epsilon; 1-epsilon];
% COMPUTE VECTOR f
% velocity
Dp = hm.D_in*(1+ F*hm.tau_p)^(1/3);
Kp = (Dp^2)*(epsilon^3)/(150*(1-epsilon)^2);
v_cost = -Kp*hm.R_gas/(hm.mu_g*hm.MH2);
v = v_cost*(dudx(1)*u(2) + dudx(2)*u(1));
% effective thermal conductivity
N = 3.08/epsilon – 1.13;
Fn = 4/3 * hm.E_prime * hm.R^(1/2) * hm.dv^(1.5);
Hv = hm.c1*hm.dv^(hm.c2);
Rs = 0.565*Hv*hm.dv/(hm.ks*Fn);
a_H = (0.75*Fn*hm.R/hm.E_prime)^(1/3);
a_LH = [];
if epsilon <= 0.47 && epsilon >= 0.01
a_LH = 1.605/sqrt(epsilon);
elseif epsilon > 0.47 && epsilon <= 1
a_LH = 3.51 – 2.51*epsilon;
else
error(‘error epsilon’);
end
a_L = a_LH*a_H;
R_L = 1/(2*hm.ks*a_L);
Pmax = 2/pi*hm.E_prime*(hm.dv/hm.R)^(0.5);
Hc = hm.c1*(1.62*hm.dv)^(hm.c2);
a1 = erfcinv(2*Pmax/Hc);
a2 = erfcinv(0.03*Pmax/Hc) – a1;
coef_a = 2*hm.b/Dp;
coef_c = -(6*(hm.gamma-1)/(9*hm.gamma-5))* (hm.kg_ref*hm.MH2/(u(1)*u(2)*hm.R_gas))*(hm.MH2*u(1)/(2*hm.kb))^(0.5);
l_m = (-1 + sqrt(1 – 4*coef_a*coef_c))/(2*coef_a);
kg = hm.kg_ref/(1+2*hm.b*l_m/Dp);
M = (((2-hm.alpha_T1)/hm.alpha_T1)+((2-hm.alpha_T2)/hm.alpha_T2))*(2*hm.gamma/(1+hm.gamma))*(1/hm.Pr)*l_m;
R_g = sqrt(2)*hm.sigma*a2/(pi * kg * a_L^2 * log(1+ a2/(a1+M/(sqrt(2)*hm.sigma))));
L = (hm.gamma+1)*3*Dp/((9*hm.gamma-5)*4*l_m*sqrt(pi));
R_G = 1/(2*pi*kg*Dp*(0.5*log(1+L) + log(1+sqrt(L)) + 1/(1+sqrt(L)) – 1));
R_mic = Rs*R_g/(Rs+R_g);
R_c_inv = 1/(R_mic+R_L) + 1/R_G;
k_eff = N*(1-epsilon)*R_c_inv/(pi*Dp);
f = [k_eff*dudx(1); -u(2)*v; 0];
% compute vector s
m_dot_a = (1-epsilon)*(hm.rho_sat-u(3))*dfdt;
s = [u(2)*hm.Cpg*v*dudx(1) + m_dot_a*hm.delta_H ; -m_dot_a+hm.phi_abs; m_dot_a];
end
function u0 = icfun(x)
run(‘HM_data.m’);
u0 = hm.ic;
end
function [pl,ql,pr,qr] = bcfun(xl, ul, xr, ur, t)
run(‘HM_data.m’);
Nu_abs = 0.3+((0.62*(hm.Re_a^(0.5))*(hm.Pr_a^(1/3)))/(1+(0.4/hm.Pr_a)^(2/3))^(1/4)*((1+(hm.Re_a/282000)^(5/8))^(4/5)));
h_f = Nu_abs*hm.kf/hm.D_tank;
pl = [0 ; 0; 0];
ql = [1; 0; 0];
pr = [h_f*(ur(1)-hm.Tf_a); 0; 0];
qr = [-1; 0; 0];
end I get this error trying to solve a system of PDEs, and I do not know if such system is solvable with ‘pdepe’. The equations are:
energy balance equation, and:
mass balance equations. Initial conditions:
Boundary conditions:
The variables in which I am solving are however T,, knowing that:
, I can write initial condition for , and I have also a value for .
I tried writing the equations in the form required by ‘pdepe’. Also, note that many parameters depends on the vector , IN particular, the term ‘v’ includes a partial spatial derivative w.r.t. P, which i transformed in a spatial derivative w.r.t. u(1) and u(2) with the above formula. Note that many constants are imported with the file.m including the initial conditions that are: This is the code of the 3 functions used inside the pde call (sorry is a bit long and messy):
function [c,f,s] = pdefun(x,t,u,dudx)
% load data
run(‘HM_data.m’);
% compute epsilon , F and dfdt
F = (hm.rho_s_in-u(3))/(u(3)*hm.tau_p – hm.rho_s_in*hm.w_max);
epsilon = 1 – (1 – hm.epsilon_0)*((1 + hm.tau_p*F)/(1 + hm.tau_a*F));
P = u(2)*u(1)*hm.R_gas/hm.MH2;
P_eq_a = (hm.C0_a*hm.C1_a*(F)^(hm.C2_a)/(1+hm.C1_a*(F)^(hm.C2_a)) + hm.C3_a*F + exp(hm.C4_a*(F-hm.C5_a)))*exp(-hm.K_a*((1/u(1))-(1/303)));
k_a = (hm.kappa_a/(1 – epsilon)) * exp(-hm.E_a/(hm.R_gas*u(1))) * log(P/P_eq_a);
dfdt = k_a*(1-F);
% compute diagonal matrix c
Cps = 6000*(3.1*hm.R + 10.04*hm.x_max*F)/(hm.Ms + 6*hm.x_max*F);
rho_C_eff = epsilon*u(2)*hm.Cpg + (1- epsilon)*u(3)*Cps;
c = [rho_C_eff; epsilon; 1-epsilon];
% COMPUTE VECTOR f
% velocity
Dp = hm.D_in*(1+ F*hm.tau_p)^(1/3);
Kp = (Dp^2)*(epsilon^3)/(150*(1-epsilon)^2);
v_cost = -Kp*hm.R_gas/(hm.mu_g*hm.MH2);
v = v_cost*(dudx(1)*u(2) + dudx(2)*u(1));
% effective thermal conductivity
N = 3.08/epsilon – 1.13;
Fn = 4/3 * hm.E_prime * hm.R^(1/2) * hm.dv^(1.5);
Hv = hm.c1*hm.dv^(hm.c2);
Rs = 0.565*Hv*hm.dv/(hm.ks*Fn);
a_H = (0.75*Fn*hm.R/hm.E_prime)^(1/3);
a_LH = [];
if epsilon <= 0.47 && epsilon >= 0.01
a_LH = 1.605/sqrt(epsilon);
elseif epsilon > 0.47 && epsilon <= 1
a_LH = 3.51 – 2.51*epsilon;
else
error(‘error epsilon’);
end
a_L = a_LH*a_H;
R_L = 1/(2*hm.ks*a_L);
Pmax = 2/pi*hm.E_prime*(hm.dv/hm.R)^(0.5);
Hc = hm.c1*(1.62*hm.dv)^(hm.c2);
a1 = erfcinv(2*Pmax/Hc);
a2 = erfcinv(0.03*Pmax/Hc) – a1;
coef_a = 2*hm.b/Dp;
coef_c = -(6*(hm.gamma-1)/(9*hm.gamma-5))* (hm.kg_ref*hm.MH2/(u(1)*u(2)*hm.R_gas))*(hm.MH2*u(1)/(2*hm.kb))^(0.5);
l_m = (-1 + sqrt(1 – 4*coef_a*coef_c))/(2*coef_a);
kg = hm.kg_ref/(1+2*hm.b*l_m/Dp);
M = (((2-hm.alpha_T1)/hm.alpha_T1)+((2-hm.alpha_T2)/hm.alpha_T2))*(2*hm.gamma/(1+hm.gamma))*(1/hm.Pr)*l_m;
R_g = sqrt(2)*hm.sigma*a2/(pi * kg * a_L^2 * log(1+ a2/(a1+M/(sqrt(2)*hm.sigma))));
L = (hm.gamma+1)*3*Dp/((9*hm.gamma-5)*4*l_m*sqrt(pi));
R_G = 1/(2*pi*kg*Dp*(0.5*log(1+L) + log(1+sqrt(L)) + 1/(1+sqrt(L)) – 1));
R_mic = Rs*R_g/(Rs+R_g);
R_c_inv = 1/(R_mic+R_L) + 1/R_G;
k_eff = N*(1-epsilon)*R_c_inv/(pi*Dp);
f = [k_eff*dudx(1); -u(2)*v; 0];
% compute vector s
m_dot_a = (1-epsilon)*(hm.rho_sat-u(3))*dfdt;
s = [u(2)*hm.Cpg*v*dudx(1) + m_dot_a*hm.delta_H ; -m_dot_a+hm.phi_abs; m_dot_a];
end
function u0 = icfun(x)
run(‘HM_data.m’);
u0 = hm.ic;
end
function [pl,ql,pr,qr] = bcfun(xl, ul, xr, ur, t)
run(‘HM_data.m’);
Nu_abs = 0.3+((0.62*(hm.Re_a^(0.5))*(hm.Pr_a^(1/3)))/(1+(0.4/hm.Pr_a)^(2/3))^(1/4)*((1+(hm.Re_a/282000)^(5/8))^(4/5)));
h_f = Nu_abs*hm.kf/hm.D_tank;
pl = [0 ; 0; 0];
ql = [1; 0; 0];
pr = [h_f*(ur(1)-hm.Tf_a); 0; 0];
qr = [-1; 0; 0];
end pde, pdepe MATLAB Answers — New Questions
How to fastly calculate this matrix operation
A matrix is a dimensional matrix, is a dimensional vector, is a dimensional matrix, is a constant.
How to fastly calculate the following three dimensional () matrix , where the element in isA matrix is a dimensional matrix, is a dimensional vector, is a dimensional matrix, is a constant.
How to fastly calculate the following three dimensional () matrix , where the element in is A matrix is a dimensional matrix, is a dimensional vector, is a dimensional matrix, is a constant.
How to fastly calculate the following three dimensional () matrix , where the element in is matrix manipulation MATLAB Answers — New Questions
Hi. Manual variant sink.
Hi. I am trying to use manual variant sink programatically using a m-script and there is an error for the parameter. Can anyone suggest me what parameter is to be used for manual variant sink as in case of manual switch it is ‘sw’?
Thanks in advance.Hi. I am trying to use manual variant sink programatically using a m-script and there is an error for the parameter. Can anyone suggest me what parameter is to be used for manual variant sink as in case of manual switch it is ‘sw’?
Thanks in advance. Hi. I am trying to use manual variant sink programatically using a m-script and there is an error for the parameter. Can anyone suggest me what parameter is to be used for manual variant sink as in case of manual switch it is ‘sw’?
Thanks in advance. manual variant sink MATLAB Answers — New Questions
matlab code to find mean, median and standard deviation of a series of data in discrete time wavelet
what is the matlab code to find mean, median and standard deviation of a series of data in discrete time wavelet platform. This data is phase currents in a machine.This is either in time series formate or in stuctured with time .what is the matlab code to find mean, median and standard deviation of a series of data in discrete time wavelet platform. This data is phase currents in a machine.This is either in time series formate or in stuctured with time . what is the matlab code to find mean, median and standard deviation of a series of data in discrete time wavelet platform. This data is phase currents in a machine.This is either in time series formate or in stuctured with time . discrete wavelet transform, mean, median, standard deviation, wavelets MATLAB Answers — New Questions
How could i import google map into matlab using mapping toolbox,with dynamic view?
I would like to import google maps into matlab by using mapping toolbox in order to edit a specific city region of the map with dynamic view.Then everytime i click on a building, matlab should be able to display some informations, which i would have provided.I would like to import google maps into matlab by using mapping toolbox in order to edit a specific city region of the map with dynamic view.Then everytime i click on a building, matlab should be able to display some informations, which i would have provided. I would like to import google maps into matlab by using mapping toolbox in order to edit a specific city region of the map with dynamic view.Then everytime i click on a building, matlab should be able to display some informations, which i would have provided. google map, mapping toolbox MATLAB Answers — New Questions
I have this problem unrecognized method property or field ‘ArduinoPrefDirFolderName’ for class ‘arduino.setup.internal.TestConnectionScreen’.
when I install the matlab support package for arduino hardware i got this error when I press test connection. Please help me fix thiswhen I install the matlab support package for arduino hardware i got this error when I press test connection. Please help me fix this when I install the matlab support package for arduino hardware i got this error when I press test connection. Please help me fix this arduino MATLAB Answers — New Questions
Test Procedure for ON/OFF Oven to Determine PID Constants
Hi.
I need some help in predefining a test procedure for a remote Oven with ON/OFF Control. A step response is unsuitable because the oven is overpowered. Will a simple ON till the temperature reaches a typical working temperature followed by on OFF be sufficient? Can the collected data then be used for analysis to determine controller constants?
If that ON/OFF test is inadequate, will multiple ON/OFF’s be better as this would fall into the category of variable input?
Thanks.Hi.
I need some help in predefining a test procedure for a remote Oven with ON/OFF Control. A step response is unsuitable because the oven is overpowered. Will a simple ON till the temperature reaches a typical working temperature followed by on OFF be sufficient? Can the collected data then be used for analysis to determine controller constants?
If that ON/OFF test is inadequate, will multiple ON/OFF’s be better as this would fall into the category of variable input?
Thanks. Hi.
I need some help in predefining a test procedure for a remote Oven with ON/OFF Control. A step response is unsuitable because the oven is overpowered. Will a simple ON till the temperature reaches a typical working temperature followed by on OFF be sufficient? Can the collected data then be used for analysis to determine controller constants?
If that ON/OFF test is inadequate, will multiple ON/OFF’s be better as this would fall into the category of variable input?
Thanks. pid, pid constants, on/off control MATLAB Answers — New Questions
how to use Deep Learning HDL Toolbox Support Package for Xilinx FPGA and SoC Devices in MATLAB ONLINE
i AM DOING RESEARCH IN DEEP NEURAL NETWORKS.SINCE OUR INSTITUTION "PSG COLLEGE OF TECHNOLOGY,COIMBATORE"I HAS LICENCE ,I HAVE STARTED TO EXPLORE DEEP LEARNING HDL TOOL BOX IN ONLINE MATLAB.BUT WHILE RUNNING THE CODE IT SHOWS THE ERROR"Attempt to create a Target for vendor ‘Xilinx’. The ‘Deep Learning HDL Toolbox Support Package for Xilinx FPGA and SoC Devices’ support package has not been installed.".since i am using ONLINe MATLAb,i could not find the Deep Learning HDL Toolbox Support Package for Xilinx FPGA and SoC Devices.i AM DOING RESEARCH IN DEEP NEURAL NETWORKS.SINCE OUR INSTITUTION "PSG COLLEGE OF TECHNOLOGY,COIMBATORE"I HAS LICENCE ,I HAVE STARTED TO EXPLORE DEEP LEARNING HDL TOOL BOX IN ONLINE MATLAB.BUT WHILE RUNNING THE CODE IT SHOWS THE ERROR"Attempt to create a Target for vendor ‘Xilinx’. The ‘Deep Learning HDL Toolbox Support Package for Xilinx FPGA and SoC Devices’ support package has not been installed.".since i am using ONLINe MATLAb,i could not find the Deep Learning HDL Toolbox Support Package for Xilinx FPGA and SoC Devices. i AM DOING RESEARCH IN DEEP NEURAL NETWORKS.SINCE OUR INSTITUTION "PSG COLLEGE OF TECHNOLOGY,COIMBATORE"I HAS LICENCE ,I HAVE STARTED TO EXPLORE DEEP LEARNING HDL TOOL BOX IN ONLINE MATLAB.BUT WHILE RUNNING THE CODE IT SHOWS THE ERROR"Attempt to create a Target for vendor ‘Xilinx’. The ‘Deep Learning HDL Toolbox Support Package for Xilinx FPGA and SoC Devices’ support package has not been installed.".since i am using ONLINe MATLAb,i could not find the Deep Learning HDL Toolbox Support Package for Xilinx FPGA and SoC Devices. deep learning hdl toolbox support package MATLAB Answers — New Questions
Using matlabFunction to run solve() in a deployed app
Hi. I’m trying to use matlabFunction to run solve() statements, so the app will be deployed fine. I have this example code:
if ~isdeployed()
syms x
eq = solve(x + 5 == 8, x);
fun = matlabFunction(eq, ‘File’, ‘SolveTest.m’);
clear x eq fun
else
fun = @SolveTest;
end
app.EditField.Value = SolveTest;
it works fine inside appdesigner, but when I try to deploy it by ‘deploytool’, I get this error:
Warning: In "test.mlapp", "syms" are excluded from packaging for the MATLAB Runtime environment according to the MATLAB Compiler license. Either remove the file or function from your code, or use the MATLAB function "isdeployed" to ensure the function is not invoked in the deployed component.
how can it be fixed?
I’m using R2022b.Hi. I’m trying to use matlabFunction to run solve() statements, so the app will be deployed fine. I have this example code:
if ~isdeployed()
syms x
eq = solve(x + 5 == 8, x);
fun = matlabFunction(eq, ‘File’, ‘SolveTest.m’);
clear x eq fun
else
fun = @SolveTest;
end
app.EditField.Value = SolveTest;
it works fine inside appdesigner, but when I try to deploy it by ‘deploytool’, I get this error:
Warning: In "test.mlapp", "syms" are excluded from packaging for the MATLAB Runtime environment according to the MATLAB Compiler license. Either remove the file or function from your code, or use the MATLAB function "isdeployed" to ensure the function is not invoked in the deployed component.
how can it be fixed?
I’m using R2022b. Hi. I’m trying to use matlabFunction to run solve() statements, so the app will be deployed fine. I have this example code:
if ~isdeployed()
syms x
eq = solve(x + 5 == 8, x);
fun = matlabFunction(eq, ‘File’, ‘SolveTest.m’);
clear x eq fun
else
fun = @SolveTest;
end
app.EditField.Value = SolveTest;
it works fine inside appdesigner, but when I try to deploy it by ‘deploytool’, I get this error:
Warning: In "test.mlapp", "syms" are excluded from packaging for the MATLAB Runtime environment according to the MATLAB Compiler license. Either remove the file or function from your code, or use the MATLAB function "isdeployed" to ensure the function is not invoked in the deployed component.
how can it be fixed?
I’m using R2022b. matlabfunction, appdesigner, solve MATLAB Answers — New Questions
Is LEGO MINDSTORMS NXT supported in previous releases?
I would like to develop algorithms to run on LEGO MINDSTORMS NXT using Simulink and I would like to know if it is supported in previous releases.I would like to develop algorithms to run on LEGO MINDSTORMS NXT using Simulink and I would like to know if it is supported in previous releases. I would like to develop algorithms to run on LEGO MINDSTORMS NXT using Simulink and I would like to know if it is supported in previous releases. lego, mindstorms, nxt, simulink MATLAB Answers — New Questions
How do I change from 24 bit depth to 8 bit depth image?
I have image data for my input 256x256x3 uint8 and the bit depth is 24. My question is, how can I change from 24 bit depth image to 8 bit depth image without changing the colortype in ‘truecolor’? Here’s my ans value. And here’s my codes to read image and info image:-
>> I=imread(‘1234.png’);
>> imfinfo(‘1234.png’)I have image data for my input 256x256x3 uint8 and the bit depth is 24. My question is, how can I change from 24 bit depth image to 8 bit depth image without changing the colortype in ‘truecolor’? Here’s my ans value. And here’s my codes to read image and info image:-
>> I=imread(‘1234.png’);
>> imfinfo(‘1234.png’) I have image data for my input 256x256x3 uint8 and the bit depth is 24. My question is, how can I change from 24 bit depth image to 8 bit depth image without changing the colortype in ‘truecolor’? Here’s my ans value. And here’s my codes to read image and info image:-
>> I=imread(‘1234.png’);
>> imfinfo(‘1234.png’) bitdepth, image processing MATLAB Answers — New Questions
Why does my native library behave differently when wrapped using clibgen?
I have a native Windows library written in C++ and compiled with the MSVC compiler which contains one class (we’ll call it MyClass since I can’t provide the actual code for security reasons). MyClass has some std::vector<double> members that serve as input data to a MyClass member function (void myFun()–no input or output args) and also has some std::vector<double> members that serve as output parameters from that function.
I have also used clibgen to build a shared library so that MATLAB can use MyClass. However, I need to make sure that the outputs I get when both matlab and C++ call the function are EXACTLY the same, which I haven’t been able to achieve. To test equality between matlab and c++ I am doing the following:
Create a MyClass object in c++ (cppobj)
Populate the input parameters of cppobj
Run myFun() from cppobj
Write the input and output (all elements) of cppobj to binary files
Load the data of these binary files into two matlab objects, one with both inputs and outputs (matobj_cppout), and one with just inputs (matobj_matout)
Run myFun() on matobj_matout
Compare the output parameters of matobj_cppout and matobj_matout
I’ve calculated small differences in the output values even though they SHOULD be exactly the same, since the function is deterministic and the input parameters in matobj_cppout and matobj_matout are identical. This leads me to believe that MATLAB is doing some kind of conversion either when the binary files or read or when the values are stored to the native MyClass object using the "clib" functionality. I’ve verified that I’m using fp:precise both when building the native library and when running clibgen.
Any ideas of what could be causing these discrepancies?I have a native Windows library written in C++ and compiled with the MSVC compiler which contains one class (we’ll call it MyClass since I can’t provide the actual code for security reasons). MyClass has some std::vector<double> members that serve as input data to a MyClass member function (void myFun()–no input or output args) and also has some std::vector<double> members that serve as output parameters from that function.
I have also used clibgen to build a shared library so that MATLAB can use MyClass. However, I need to make sure that the outputs I get when both matlab and C++ call the function are EXACTLY the same, which I haven’t been able to achieve. To test equality between matlab and c++ I am doing the following:
Create a MyClass object in c++ (cppobj)
Populate the input parameters of cppobj
Run myFun() from cppobj
Write the input and output (all elements) of cppobj to binary files
Load the data of these binary files into two matlab objects, one with both inputs and outputs (matobj_cppout), and one with just inputs (matobj_matout)
Run myFun() on matobj_matout
Compare the output parameters of matobj_cppout and matobj_matout
I’ve calculated small differences in the output values even though they SHOULD be exactly the same, since the function is deterministic and the input parameters in matobj_cppout and matobj_matout are identical. This leads me to believe that MATLAB is doing some kind of conversion either when the binary files or read or when the values are stored to the native MyClass object using the "clib" functionality. I’ve verified that I’m using fp:precise both when building the native library and when running clibgen.
Any ideas of what could be causing these discrepancies? I have a native Windows library written in C++ and compiled with the MSVC compiler which contains one class (we’ll call it MyClass since I can’t provide the actual code for security reasons). MyClass has some std::vector<double> members that serve as input data to a MyClass member function (void myFun()–no input or output args) and also has some std::vector<double> members that serve as output parameters from that function.
I have also used clibgen to build a shared library so that MATLAB can use MyClass. However, I need to make sure that the outputs I get when both matlab and C++ call the function are EXACTLY the same, which I haven’t been able to achieve. To test equality between matlab and c++ I am doing the following:
Create a MyClass object in c++ (cppobj)
Populate the input parameters of cppobj
Run myFun() from cppobj
Write the input and output (all elements) of cppobj to binary files
Load the data of these binary files into two matlab objects, one with both inputs and outputs (matobj_cppout), and one with just inputs (matobj_matout)
Run myFun() on matobj_matout
Compare the output parameters of matobj_cppout and matobj_matout
I’ve calculated small differences in the output values even though they SHOULD be exactly the same, since the function is deterministic and the input parameters in matobj_cppout and matobj_matout are identical. This leads me to believe that MATLAB is doing some kind of conversion either when the binary files or read or when the values are stored to the native MyClass object using the "clib" functionality. I’ve verified that I’m using fp:precise both when building the native library and when running clibgen.
Any ideas of what could be causing these discrepancies? clibgen, native, c++ MATLAB Answers — New Questions
How compute connected node with each main node
I need to compute the totl number of green nodes and red nodes connected with each main node as shown in figure below
the code is in below
%Composing fixed nodes t
for h=1:1:num_fixed_nodes
S1(h).xd = position_region(i,1) + fixed_nodes{i, 1}(h,1);
S1(h).yd = position_region(i,2) + fixed_nodes{i, 1}(h,2);
S1(h).distance = sqrt((S1(h).xd-bs_x)^2 + (S1(h).yd-bs_y)^2 );
S1(h).G=0;
S1(h).id=h;
S1(h).type=’C’;
S1(h).temp = interp2(aa1,bb1,temp_values_1,S1(h).xd,S1(h).yd);
S1(h).E = Eo*rand(1,1);
Et=Et+S1(h).E;
S1(h).node_status = S1(h).temp<thresh_temp;
if(S1(h).node_status==1)
scatter(S1(h).xd,S1(h).yd, ‘filled’,’MarkerFaceColor’,’m’);
else
scatter(S1(h).xd,S1(h).yd, ‘filled’,’MarkerFaceColor’,’r’);
end
end
%Composing random nodes that will then communicate with fixed
%nodes
for h=num_fixed_nodes+1:1:num_nodes
%Generate random nodes within the region
S1(h).xd = position_region(i,1) + rand(1,1)*region_width;
S1(h).yd = position_region(i,2) + rand(1,1)*region_height;
S1(h).G=0;
S1(h).id=h;
S1(h).type=’N’;
S1(h).temp = interp2(aa1,bb1,temp_values_1,S1(h).xd,S1(h).yd);
S1(h).E=Eo*rand(1,1);
Et=Et+S1(h).E;
S1(h).node_status = S1(h).temp<thresh_temp;
if(S1(h).node_status==1)
scatter(S1(h).xd,S1(h).yd, ‘filled’,’MarkerFaceColor’,’g’);
else
scatter(S1(h).xd,S1(h).yd, ‘filled’,’MarkerFaceColor’,’r’);
endI need to compute the totl number of green nodes and red nodes connected with each main node as shown in figure below
the code is in below
%Composing fixed nodes t
for h=1:1:num_fixed_nodes
S1(h).xd = position_region(i,1) + fixed_nodes{i, 1}(h,1);
S1(h).yd = position_region(i,2) + fixed_nodes{i, 1}(h,2);
S1(h).distance = sqrt((S1(h).xd-bs_x)^2 + (S1(h).yd-bs_y)^2 );
S1(h).G=0;
S1(h).id=h;
S1(h).type=’C’;
S1(h).temp = interp2(aa1,bb1,temp_values_1,S1(h).xd,S1(h).yd);
S1(h).E = Eo*rand(1,1);
Et=Et+S1(h).E;
S1(h).node_status = S1(h).temp<thresh_temp;
if(S1(h).node_status==1)
scatter(S1(h).xd,S1(h).yd, ‘filled’,’MarkerFaceColor’,’m’);
else
scatter(S1(h).xd,S1(h).yd, ‘filled’,’MarkerFaceColor’,’r’);
end
end
%Composing random nodes that will then communicate with fixed
%nodes
for h=num_fixed_nodes+1:1:num_nodes
%Generate random nodes within the region
S1(h).xd = position_region(i,1) + rand(1,1)*region_width;
S1(h).yd = position_region(i,2) + rand(1,1)*region_height;
S1(h).G=0;
S1(h).id=h;
S1(h).type=’N’;
S1(h).temp = interp2(aa1,bb1,temp_values_1,S1(h).xd,S1(h).yd);
S1(h).E=Eo*rand(1,1);
Et=Et+S1(h).E;
S1(h).node_status = S1(h).temp<thresh_temp;
if(S1(h).node_status==1)
scatter(S1(h).xd,S1(h).yd, ‘filled’,’MarkerFaceColor’,’g’);
else
scatter(S1(h).xd,S1(h).yd, ‘filled’,’MarkerFaceColor’,’r’);
end I need to compute the totl number of green nodes and red nodes connected with each main node as shown in figure below
the code is in below
%Composing fixed nodes t
for h=1:1:num_fixed_nodes
S1(h).xd = position_region(i,1) + fixed_nodes{i, 1}(h,1);
S1(h).yd = position_region(i,2) + fixed_nodes{i, 1}(h,2);
S1(h).distance = sqrt((S1(h).xd-bs_x)^2 + (S1(h).yd-bs_y)^2 );
S1(h).G=0;
S1(h).id=h;
S1(h).type=’C’;
S1(h).temp = interp2(aa1,bb1,temp_values_1,S1(h).xd,S1(h).yd);
S1(h).E = Eo*rand(1,1);
Et=Et+S1(h).E;
S1(h).node_status = S1(h).temp<thresh_temp;
if(S1(h).node_status==1)
scatter(S1(h).xd,S1(h).yd, ‘filled’,’MarkerFaceColor’,’m’);
else
scatter(S1(h).xd,S1(h).yd, ‘filled’,’MarkerFaceColor’,’r’);
end
end
%Composing random nodes that will then communicate with fixed
%nodes
for h=num_fixed_nodes+1:1:num_nodes
%Generate random nodes within the region
S1(h).xd = position_region(i,1) + rand(1,1)*region_width;
S1(h).yd = position_region(i,2) + rand(1,1)*region_height;
S1(h).G=0;
S1(h).id=h;
S1(h).type=’N’;
S1(h).temp = interp2(aa1,bb1,temp_values_1,S1(h).xd,S1(h).yd);
S1(h).E=Eo*rand(1,1);
Et=Et+S1(h).E;
S1(h).node_status = S1(h).temp<thresh_temp;
if(S1(h).node_status==1)
scatter(S1(h).xd,S1(h).yd, ‘filled’,’MarkerFaceColor’,’g’);
else
scatter(S1(h).xd,S1(h).yd, ‘filled’,’MarkerFaceColor’,’r’);
end plotting, sum MATLAB Answers — New Questions
Could anyone tell me an accelerometer that will connect to matlab please?
Looking for accelerometers that I can buy online that will connect to matlabLooking for accelerometers that I can buy online that will connect to matlab Looking for accelerometers that I can buy online that will connect to matlab accelerometer, data acquisition, vibration MATLAB Answers — New Questions
How do I load pretrained LSTM models (in .mat format) into a MATLAB Function Block in Simulink?
I am trying to create a Simulink model for SoC and SoH prediction of a battery, in order to implement in Hardware-in-the-loop(HIL) using OP4512 simulator. Using Stateful predict block in MATLAB 2021a did not help as RT-LAB did not support the block during the real-time simulation. So, I have tried using a user defined function block for loading two LSTM models, which were trained using M code and has an RMSE of 0.2365%. I have used the following code for my function block:
function [soc,soh] = prediction(voltage, current, temperature, cycles)
persistent lstmNet lstmNet1;
if isempty(lstmNet)
lstmNet = coder.loadDeepLearningNetwork(‘net25_3layer.mat’);
end
if isempty(lstmNet1)
lstmNet1 = coder.loadDeepLearningNetwork(’12_5_23_SOH_optimised_3layer.mat’);
end
norm_voltage = (voltage-3.9102)/0.137;
norm_current = (current+0.8334)/2.4459;
norm_temperature = (temperature-24.5092)/0.3066;
norm_cycles = (cycles-174.9916)/114.5728;
X = [norm_voltage, norm_current, norm_temperature];
norm_soc = predict(lstmNet, X’);
soc = norm_soc*21.612+63.0467;
X1 = [norm_soc, norm_cycles];
norm_soh = predict(lstmNet1, X1′);
soh = norm_soh*33.6548+46.8586;
end
Also, I have set my simulation target language as C++.
When I run this model, I find that the output is in a different range (in case of SoC, 63% – 78%) than the output data in the dataset (In case of SoC, 28%-100%). This, I believe, is because the loadDeepLearningNetwork do not consider any recurrent computation in the network (correct me if I am wrong).
Is there any way I can load these two LSTM models into the function block such that I get the appropriate output?
Find the two .mat files (LSTM models) and the input variables in soc_sim_ws.mat file.I am trying to create a Simulink model for SoC and SoH prediction of a battery, in order to implement in Hardware-in-the-loop(HIL) using OP4512 simulator. Using Stateful predict block in MATLAB 2021a did not help as RT-LAB did not support the block during the real-time simulation. So, I have tried using a user defined function block for loading two LSTM models, which were trained using M code and has an RMSE of 0.2365%. I have used the following code for my function block:
function [soc,soh] = prediction(voltage, current, temperature, cycles)
persistent lstmNet lstmNet1;
if isempty(lstmNet)
lstmNet = coder.loadDeepLearningNetwork(‘net25_3layer.mat’);
end
if isempty(lstmNet1)
lstmNet1 = coder.loadDeepLearningNetwork(’12_5_23_SOH_optimised_3layer.mat’);
end
norm_voltage = (voltage-3.9102)/0.137;
norm_current = (current+0.8334)/2.4459;
norm_temperature = (temperature-24.5092)/0.3066;
norm_cycles = (cycles-174.9916)/114.5728;
X = [norm_voltage, norm_current, norm_temperature];
norm_soc = predict(lstmNet, X’);
soc = norm_soc*21.612+63.0467;
X1 = [norm_soc, norm_cycles];
norm_soh = predict(lstmNet1, X1′);
soh = norm_soh*33.6548+46.8586;
end
Also, I have set my simulation target language as C++.
When I run this model, I find that the output is in a different range (in case of SoC, 63% – 78%) than the output data in the dataset (In case of SoC, 28%-100%). This, I believe, is because the loadDeepLearningNetwork do not consider any recurrent computation in the network (correct me if I am wrong).
Is there any way I can load these two LSTM models into the function block such that I get the appropriate output?
Find the two .mat files (LSTM models) and the input variables in soc_sim_ws.mat file. I am trying to create a Simulink model for SoC and SoH prediction of a battery, in order to implement in Hardware-in-the-loop(HIL) using OP4512 simulator. Using Stateful predict block in MATLAB 2021a did not help as RT-LAB did not support the block during the real-time simulation. So, I have tried using a user defined function block for loading two LSTM models, which were trained using M code and has an RMSE of 0.2365%. I have used the following code for my function block:
function [soc,soh] = prediction(voltage, current, temperature, cycles)
persistent lstmNet lstmNet1;
if isempty(lstmNet)
lstmNet = coder.loadDeepLearningNetwork(‘net25_3layer.mat’);
end
if isempty(lstmNet1)
lstmNet1 = coder.loadDeepLearningNetwork(’12_5_23_SOH_optimised_3layer.mat’);
end
norm_voltage = (voltage-3.9102)/0.137;
norm_current = (current+0.8334)/2.4459;
norm_temperature = (temperature-24.5092)/0.3066;
norm_cycles = (cycles-174.9916)/114.5728;
X = [norm_voltage, norm_current, norm_temperature];
norm_soc = predict(lstmNet, X’);
soc = norm_soc*21.612+63.0467;
X1 = [norm_soc, norm_cycles];
norm_soh = predict(lstmNet1, X1′);
soh = norm_soh*33.6548+46.8586;
end
Also, I have set my simulation target language as C++.
When I run this model, I find that the output is in a different range (in case of SoC, 63% – 78%) than the output data in the dataset (In case of SoC, 28%-100%). This, I believe, is because the loadDeepLearningNetwork do not consider any recurrent computation in the network (correct me if I am wrong).
Is there any way I can load these two LSTM models into the function block such that I get the appropriate output?
Find the two .mat files (LSTM models) and the input variables in soc_sim_ws.mat file. simulink, lstm, function block MATLAB Answers — New Questions
how do i put MATLAB complex array to python?
I want to python code in MATLAB.(use pyrunfile)
I can’t put complex array into python.
I’m testing very simple code.
python core:(file :Test.py):
print(indat)
MATLAB(inline input):
adat=xxx
pyrunfile("./Test.py",indat=adat)
I got results under below.
Case A: input adat=1+0.1i (MATALB inline)
output (1+0.1j)
Case A is OK
Case B: input adat=[1,2,3]
output array(‘d’, [1.0, 2.0, 3.0])
Case B is OK
Case C:input adat=[1+0.1i,2+0.2i,3+0.3i]
Output <memory at 0x000002562F9E9560>
Why?
what should i do put complex array into python?I want to python code in MATLAB.(use pyrunfile)
I can’t put complex array into python.
I’m testing very simple code.
python core:(file :Test.py):
print(indat)
MATLAB(inline input):
adat=xxx
pyrunfile("./Test.py",indat=adat)
I got results under below.
Case A: input adat=1+0.1i (MATALB inline)
output (1+0.1j)
Case A is OK
Case B: input adat=[1,2,3]
output array(‘d’, [1.0, 2.0, 3.0])
Case B is OK
Case C:input adat=[1+0.1i,2+0.2i,3+0.3i]
Output <memory at 0x000002562F9E9560>
Why?
what should i do put complex array into python? I want to python code in MATLAB.(use pyrunfile)
I can’t put complex array into python.
I’m testing very simple code.
python core:(file :Test.py):
print(indat)
MATLAB(inline input):
adat=xxx
pyrunfile("./Test.py",indat=adat)
I got results under below.
Case A: input adat=1+0.1i (MATALB inline)
output (1+0.1j)
Case A is OK
Case B: input adat=[1,2,3]
output array(‘d’, [1.0, 2.0, 3.0])
Case B is OK
Case C:input adat=[1+0.1i,2+0.2i,3+0.3i]
Output <memory at 0x000002562F9E9560>
Why?
what should i do put complex array into python? complex array, python MATLAB Answers — New Questions
How can I keep a function operating while the screen is locked?
using MATLAB 2014A on macOS 10.11.6, I am running a function from the command line, and when I lock the screen the function suspends activity until I unlock it again.using MATLAB 2014A on macOS 10.11.6, I am running a function from the command line, and when I lock the screen the function suspends activity until I unlock it again. using MATLAB 2014A on macOS 10.11.6, I am running a function from the command line, and when I lock the screen the function suspends activity until I unlock it again. suspended job MATLAB Answers — New Questions
genetic algorithm with integer constraint returns non-integer solution
I was solving the Four Peaks Problem[1] using ga function in MATLAB.
This problem has an integer constraint and the solution is expected to be in integers, but MATLAB’s ga function returns non-integer solution, although they are almost integers.
Following is the code. I expect to see
oz = [21.0000 79.0000], and fval = -179.0000
but I see
oz = [21.0000 79.0010], and fval = -179.0010
Well, they are, like I said, almost integers, so I surely can interpret them, but I was just curious. Is this expected output? Or is the function missing something during the postprocessing?
%% Setup
close
clear
clc
% for reproducibility
rng(1,’twister’)
% length of the bit string
N = 100;
% threshold
T = 20;
% Pass in fixed parameters to objfun.
objfun = @(oz) -four_peaks(oz,T,N);
% constraints
INTCON = [1, 1]; % integer constraints: Both o and z must be integers.
lb = [0, 0]; % Both o and z cannot be negative.
ub = [N,N]; % Both o and z cannot be greater than the length of the bit string.
A = [1,1]; b = N; % inequality constraint: o+z < length of the bit string.
%% Solve
% Solve
[oz,fval,exitflag,output,population,scores] = ga(objfun,2,A,b,[],[],lb,ub,[],INTCON);
%% Report
disp(oz)
disp(-fval)
%% Object Function
function y = four_peaks(x, T, N)
if x(1) > T && x(2) > T
y = max(x) + N;
else
y = max(x);
end
endI was solving the Four Peaks Problem[1] using ga function in MATLAB.
This problem has an integer constraint and the solution is expected to be in integers, but MATLAB’s ga function returns non-integer solution, although they are almost integers.
Following is the code. I expect to see
oz = [21.0000 79.0000], and fval = -179.0000
but I see
oz = [21.0000 79.0010], and fval = -179.0010
Well, they are, like I said, almost integers, so I surely can interpret them, but I was just curious. Is this expected output? Or is the function missing something during the postprocessing?
%% Setup
close
clear
clc
% for reproducibility
rng(1,’twister’)
% length of the bit string
N = 100;
% threshold
T = 20;
% Pass in fixed parameters to objfun.
objfun = @(oz) -four_peaks(oz,T,N);
% constraints
INTCON = [1, 1]; % integer constraints: Both o and z must be integers.
lb = [0, 0]; % Both o and z cannot be negative.
ub = [N,N]; % Both o and z cannot be greater than the length of the bit string.
A = [1,1]; b = N; % inequality constraint: o+z < length of the bit string.
%% Solve
% Solve
[oz,fval,exitflag,output,population,scores] = ga(objfun,2,A,b,[],[],lb,ub,[],INTCON);
%% Report
disp(oz)
disp(-fval)
%% Object Function
function y = four_peaks(x, T, N)
if x(1) > T && x(2) > T
y = max(x) + N;
else
y = max(x);
end
end I was solving the Four Peaks Problem[1] using ga function in MATLAB.
This problem has an integer constraint and the solution is expected to be in integers, but MATLAB’s ga function returns non-integer solution, although they are almost integers.
Following is the code. I expect to see
oz = [21.0000 79.0000], and fval = -179.0000
but I see
oz = [21.0000 79.0010], and fval = -179.0010
Well, they are, like I said, almost integers, so I surely can interpret them, but I was just curious. Is this expected output? Or is the function missing something during the postprocessing?
%% Setup
close
clear
clc
% for reproducibility
rng(1,’twister’)
% length of the bit string
N = 100;
% threshold
T = 20;
% Pass in fixed parameters to objfun.
objfun = @(oz) -four_peaks(oz,T,N);
% constraints
INTCON = [1, 1]; % integer constraints: Both o and z must be integers.
lb = [0, 0]; % Both o and z cannot be negative.
ub = [N,N]; % Both o and z cannot be greater than the length of the bit string.
A = [1,1]; b = N; % inequality constraint: o+z < length of the bit string.
%% Solve
% Solve
[oz,fval,exitflag,output,population,scores] = ga(objfun,2,A,b,[],[],lb,ub,[],INTCON);
%% Report
disp(oz)
disp(-fval)
%% Object Function
function y = four_peaks(x, T, N)
if x(1) > T && x(2) > T
y = max(x) + N;
else
y = max(x);
end
end genetic algorithm, four peaks problem MATLAB Answers — New Questions
出现需要使用’Level-2 MATLAB S-Function’的错误
S函数程序为
function calculate_gain_sfunc(block)
setup(block);
end
function setup(block)
% Register the number of ports
block.NumInputPorts = 3; % Q1, Q2, and F2(t)
block.NumOutputPorts = 2; % y1 and y2
% Set up the input and output ports
block.SetPreCompInpPortInfoToDynamic;
block.SetPreCompOutPortInfoToDynamic;
block.InputPort(1).Dimensions = 1; % Q1
block.InputPort(2).Dimensions = 1; % Q2
block.InputPort(3).Dimensions = 1; % F2
block.OutputPort(1).Dimensions = 1; % y1
block.OutputPort(2).Dimensions = 1; % y2
% Register the parameters
block.NumDialogPrms = 9; % J1, L1, R1, AE, F1, J2, L2, R2
% Set the sample time
block.SampleTimes = [0 0];
% Specify the block simStateCompliance
block.SimStateCompliance = ‘DefaultSimState’;
% Register the methods
block.RegBlockMethod(‘Outputs’, @Output); % Register the output method
end
function Output(block)
% Get the inputs
Q1 = block.InputPort(1).Data;
Q2 = block.InputPort(2).Data;
F2 = block.InputPort(3).Data;
% Get the parameters
J1 = block.DialogPrm(1).Data;
L1 = block.DialogPrm(2).Data;
R1 = block.DialogPrm(3).Data;
AE = block.DialogPrm(4).Data;
F1 = block.DialogPrm(5).Data;
J2 = block.DialogPrm(6).Data;
L2 = block.DialogPrm(7).Data;
R2 = block.DialogPrm(8).Data;
% Compute the gains
gain1 = J1 * L1 / (R1 * (AE – F1));
gain2 = J2 * L2 / (R2 * (F2 – AE));
% Compute the outputs
y1 = gain1 * Q1;
y2 = gain2 * Q2;
% Set the outputs
block.OutputPort(1).Data = y1;
block.OutputPort(2).Data = y2;
end
模型如上图所示
simulink运行结果为:
The specified MATLAB File ‘calculate_gain_sfunc’ in ‘sys/S-Function1’ is not a valid Level 1 S-Function. The number of input and/or output arguments is not valid. A likely cause for this error is that the name of a level-2 MATLAB S-function has been specified in this block (which supports only level-1 MATLAB S-functions). Use the ‘Level-2 MATLAB S-Function’ block from the Simulink libraryS函数程序为
function calculate_gain_sfunc(block)
setup(block);
end
function setup(block)
% Register the number of ports
block.NumInputPorts = 3; % Q1, Q2, and F2(t)
block.NumOutputPorts = 2; % y1 and y2
% Set up the input and output ports
block.SetPreCompInpPortInfoToDynamic;
block.SetPreCompOutPortInfoToDynamic;
block.InputPort(1).Dimensions = 1; % Q1
block.InputPort(2).Dimensions = 1; % Q2
block.InputPort(3).Dimensions = 1; % F2
block.OutputPort(1).Dimensions = 1; % y1
block.OutputPort(2).Dimensions = 1; % y2
% Register the parameters
block.NumDialogPrms = 9; % J1, L1, R1, AE, F1, J2, L2, R2
% Set the sample time
block.SampleTimes = [0 0];
% Specify the block simStateCompliance
block.SimStateCompliance = ‘DefaultSimState’;
% Register the methods
block.RegBlockMethod(‘Outputs’, @Output); % Register the output method
end
function Output(block)
% Get the inputs
Q1 = block.InputPort(1).Data;
Q2 = block.InputPort(2).Data;
F2 = block.InputPort(3).Data;
% Get the parameters
J1 = block.DialogPrm(1).Data;
L1 = block.DialogPrm(2).Data;
R1 = block.DialogPrm(3).Data;
AE = block.DialogPrm(4).Data;
F1 = block.DialogPrm(5).Data;
J2 = block.DialogPrm(6).Data;
L2 = block.DialogPrm(7).Data;
R2 = block.DialogPrm(8).Data;
% Compute the gains
gain1 = J1 * L1 / (R1 * (AE – F1));
gain2 = J2 * L2 / (R2 * (F2 – AE));
% Compute the outputs
y1 = gain1 * Q1;
y2 = gain2 * Q2;
% Set the outputs
block.OutputPort(1).Data = y1;
block.OutputPort(2).Data = y2;
end
模型如上图所示
simulink运行结果为:
The specified MATLAB File ‘calculate_gain_sfunc’ in ‘sys/S-Function1’ is not a valid Level 1 S-Function. The number of input and/or output arguments is not valid. A likely cause for this error is that the name of a level-2 MATLAB S-function has been specified in this block (which supports only level-1 MATLAB S-functions). Use the ‘Level-2 MATLAB S-Function’ block from the Simulink library S函数程序为
function calculate_gain_sfunc(block)
setup(block);
end
function setup(block)
% Register the number of ports
block.NumInputPorts = 3; % Q1, Q2, and F2(t)
block.NumOutputPorts = 2; % y1 and y2
% Set up the input and output ports
block.SetPreCompInpPortInfoToDynamic;
block.SetPreCompOutPortInfoToDynamic;
block.InputPort(1).Dimensions = 1; % Q1
block.InputPort(2).Dimensions = 1; % Q2
block.InputPort(3).Dimensions = 1; % F2
block.OutputPort(1).Dimensions = 1; % y1
block.OutputPort(2).Dimensions = 1; % y2
% Register the parameters
block.NumDialogPrms = 9; % J1, L1, R1, AE, F1, J2, L2, R2
% Set the sample time
block.SampleTimes = [0 0];
% Specify the block simStateCompliance
block.SimStateCompliance = ‘DefaultSimState’;
% Register the methods
block.RegBlockMethod(‘Outputs’, @Output); % Register the output method
end
function Output(block)
% Get the inputs
Q1 = block.InputPort(1).Data;
Q2 = block.InputPort(2).Data;
F2 = block.InputPort(3).Data;
% Get the parameters
J1 = block.DialogPrm(1).Data;
L1 = block.DialogPrm(2).Data;
R1 = block.DialogPrm(3).Data;
AE = block.DialogPrm(4).Data;
F1 = block.DialogPrm(5).Data;
J2 = block.DialogPrm(6).Data;
L2 = block.DialogPrm(7).Data;
R2 = block.DialogPrm(8).Data;
% Compute the gains
gain1 = J1 * L1 / (R1 * (AE – F1));
gain2 = J2 * L2 / (R2 * (F2 – AE));
% Compute the outputs
y1 = gain1 * Q1;
y2 = gain2 * Q2;
% Set the outputs
block.OutputPort(1).Data = y1;
block.OutputPort(2).Data = y2;
end
模型如上图所示
simulink运行结果为:
The specified MATLAB File ‘calculate_gain_sfunc’ in ‘sys/S-Function1’ is not a valid Level 1 S-Function. The number of input and/or output arguments is not valid. A likely cause for this error is that the name of a level-2 MATLAB S-function has been specified in this block (which supports only level-1 MATLAB S-functions). Use the ‘Level-2 MATLAB S-Function’ block from the Simulink library 2018b MATLAB Answers — New Questions
how to combined monthly data in sequence?
hello all,
I have a time series data for 10 year (lat X lon X 120). For taking monthly mean of every jan, feb…. dec, I have separated jan to dec data
jan = data(:,:,[1:12:10]);
feb = data(:,:,[2:12:10]);
mar = data(:,:,[3:12:10]);
aprl = data(:,:,[4:12:10]);………..
Then I have taken mean,
jan_mean = mean(jan(:,:,1:10),3); feb_mean = mean(feb(:,:,1:10),3); ……………………………………………….dec_mean = mean(dec(:,:,1:10),3);
For calculation of anomaly, Anom_jan = jan – jan_mean, Anom_feb = feb – feb_mean………………….
Now, I want to combined all these Anom timeseries in seuence (lat X lon X 120).
how to merge theses timeseries in sequece of jan to dec of ever year?hello all,
I have a time series data for 10 year (lat X lon X 120). For taking monthly mean of every jan, feb…. dec, I have separated jan to dec data
jan = data(:,:,[1:12:10]);
feb = data(:,:,[2:12:10]);
mar = data(:,:,[3:12:10]);
aprl = data(:,:,[4:12:10]);………..
Then I have taken mean,
jan_mean = mean(jan(:,:,1:10),3); feb_mean = mean(feb(:,:,1:10),3); ……………………………………………….dec_mean = mean(dec(:,:,1:10),3);
For calculation of anomaly, Anom_jan = jan – jan_mean, Anom_feb = feb – feb_mean………………….
Now, I want to combined all these Anom timeseries in seuence (lat X lon X 120).
how to merge theses timeseries in sequece of jan to dec of ever year? hello all,
I have a time series data for 10 year (lat X lon X 120). For taking monthly mean of every jan, feb…. dec, I have separated jan to dec data
jan = data(:,:,[1:12:10]);
feb = data(:,:,[2:12:10]);
mar = data(:,:,[3:12:10]);
aprl = data(:,:,[4:12:10]);………..
Then I have taken mean,
jan_mean = mean(jan(:,:,1:10),3); feb_mean = mean(feb(:,:,1:10),3); ……………………………………………….dec_mean = mean(dec(:,:,1:10),3);
For calculation of anomaly, Anom_jan = jan – jan_mean, Anom_feb = feb – feb_mean………………….
Now, I want to combined all these Anom timeseries in seuence (lat X lon X 120).
how to merge theses timeseries in sequece of jan to dec of ever year? time series, matrix, vector MATLAB Answers — New Questions