Tag Archives: matlab
how to run two scripts (one MEX function and one that calls upon a digital library) in parallel using parfor and parallel pools?
I am trying to achieve parallel exectution of two functions that would normally need two separate matlab(r2023a) windows open in order to successfuly execute. The PULM() function is a MEX funtion and DMD_Funtion is a matlab function that uses a library (‘alp4395’). because of this, a threadpool does not work because ‘Use of MEX functions is not supported on a thread-based worker.’ The reason either a parallel processing function or 2 MATLAB windows are needed is because PULM() needs to initialize into its ready state then it waits for a trigger (meaning matlab will stay ‘Busy’ and wont execute any other commands until PULM is finished, which waits indefinitely for a trigger), which is the DMD_Function(), then finishes the rest of the PULM MEX functions to collect data and exit.
I have been trying to use parpool(‘Processes’) instead, however it seems that it is unable to successfully ‘exit gracefully’ from the C code, probably due to timing issues with the trigger. PULM() needs several seconds to be in its ‘Ready’ state before the DMD_Funtion() is ran or else the PULM() will be waiting for triggers and basically never exit. I noticed that even with the Pause(5) or even 10 that I put in before the the DMD funtion is ran, all that happens is that both workers wait 5 seconds before running simultaneously instead of being delayed like they are supposed to. How do I delay the exectution of the DMD funtion? is it possible that i = 3 is running before i = 2 causing both of interations to pause before executing? Are there any other ways I could accomplish parallel processing in this way without the use of 2 MATLAB windows?
Initially I thought using 2 push buttons on a GUI would work because I believed that push buttons worked in parallel, but I soon realized they were executed sequentially. So I now have to use parallel toolbox functions so that one push button with execute everything in parallel.
Here’s the code for the parfor:
parfor i=2:3
if i==2
%pause(1);
%DMD_Function(patternNumber,AvergeNumber,SamplingRate,BinningNumber=768/SamplingRate) 768/32
PULM(PonitNumber,NumberofRecording,AvergeNumber);
%disp(‘run 1’);
disp(‘done 1’)
else
pause(5);
DMD_Function(patternNumber,AvergeNumber,SamplingRate,BinningNumber);
disp(‘done 2’)
end
end
Note: ‘done 1’ does not display meaning it does not complete the MEX function but ‘All records were saved to file’ does display which is right before ‘Exit Gracefully’ for the Digitizer.
In a separate code I tried to why the pause was just making them both wait 5 seconds then simultaneously running instead of being delayed. Threadpools seem to do what I want, it executes i==2 and delays i == 3. I am still not sure as to why Processespools doesn’t do the same thing. I even tried to stop the execution of the i==3 line by having it check if i == 2 wrote in a file, but it just ccomes up with an error. this is the reason why I was wondering if i==3 is being executed faster than i==2.
stateFile = ‘state.txt’;
parfor i = 2:3
if i == 2
%pause(5);
disp(‘hi’)
fid = fopen(stateFile, ‘w’);
fwrite(fid, ‘5’, ‘char’);
fclose(fid);
else
pause(5);
if isfile(stateFile)
fid = fopen(stateFile, ‘r’);
state = fread(fid, ‘*char’)’;
fclose(fid);
% Check if the state value matches
if strcmp(state, ‘5’)
disp(‘hello’);
end
end
end
endI am trying to achieve parallel exectution of two functions that would normally need two separate matlab(r2023a) windows open in order to successfuly execute. The PULM() function is a MEX funtion and DMD_Funtion is a matlab function that uses a library (‘alp4395’). because of this, a threadpool does not work because ‘Use of MEX functions is not supported on a thread-based worker.’ The reason either a parallel processing function or 2 MATLAB windows are needed is because PULM() needs to initialize into its ready state then it waits for a trigger (meaning matlab will stay ‘Busy’ and wont execute any other commands until PULM is finished, which waits indefinitely for a trigger), which is the DMD_Function(), then finishes the rest of the PULM MEX functions to collect data and exit.
I have been trying to use parpool(‘Processes’) instead, however it seems that it is unable to successfully ‘exit gracefully’ from the C code, probably due to timing issues with the trigger. PULM() needs several seconds to be in its ‘Ready’ state before the DMD_Funtion() is ran or else the PULM() will be waiting for triggers and basically never exit. I noticed that even with the Pause(5) or even 10 that I put in before the the DMD funtion is ran, all that happens is that both workers wait 5 seconds before running simultaneously instead of being delayed like they are supposed to. How do I delay the exectution of the DMD funtion? is it possible that i = 3 is running before i = 2 causing both of interations to pause before executing? Are there any other ways I could accomplish parallel processing in this way without the use of 2 MATLAB windows?
Initially I thought using 2 push buttons on a GUI would work because I believed that push buttons worked in parallel, but I soon realized they were executed sequentially. So I now have to use parallel toolbox functions so that one push button with execute everything in parallel.
Here’s the code for the parfor:
parfor i=2:3
if i==2
%pause(1);
%DMD_Function(patternNumber,AvergeNumber,SamplingRate,BinningNumber=768/SamplingRate) 768/32
PULM(PonitNumber,NumberofRecording,AvergeNumber);
%disp(‘run 1’);
disp(‘done 1’)
else
pause(5);
DMD_Function(patternNumber,AvergeNumber,SamplingRate,BinningNumber);
disp(‘done 2’)
end
end
Note: ‘done 1’ does not display meaning it does not complete the MEX function but ‘All records were saved to file’ does display which is right before ‘Exit Gracefully’ for the Digitizer.
In a separate code I tried to why the pause was just making them both wait 5 seconds then simultaneously running instead of being delayed. Threadpools seem to do what I want, it executes i==2 and delays i == 3. I am still not sure as to why Processespools doesn’t do the same thing. I even tried to stop the execution of the i==3 line by having it check if i == 2 wrote in a file, but it just ccomes up with an error. this is the reason why I was wondering if i==3 is being executed faster than i==2.
stateFile = ‘state.txt’;
parfor i = 2:3
if i == 2
%pause(5);
disp(‘hi’)
fid = fopen(stateFile, ‘w’);
fwrite(fid, ‘5’, ‘char’);
fclose(fid);
else
pause(5);
if isfile(stateFile)
fid = fopen(stateFile, ‘r’);
state = fread(fid, ‘*char’)’;
fclose(fid);
% Check if the state value matches
if strcmp(state, ‘5’)
disp(‘hello’);
end
end
end
end I am trying to achieve parallel exectution of two functions that would normally need two separate matlab(r2023a) windows open in order to successfuly execute. The PULM() function is a MEX funtion and DMD_Funtion is a matlab function that uses a library (‘alp4395’). because of this, a threadpool does not work because ‘Use of MEX functions is not supported on a thread-based worker.’ The reason either a parallel processing function or 2 MATLAB windows are needed is because PULM() needs to initialize into its ready state then it waits for a trigger (meaning matlab will stay ‘Busy’ and wont execute any other commands until PULM is finished, which waits indefinitely for a trigger), which is the DMD_Function(), then finishes the rest of the PULM MEX functions to collect data and exit.
I have been trying to use parpool(‘Processes’) instead, however it seems that it is unable to successfully ‘exit gracefully’ from the C code, probably due to timing issues with the trigger. PULM() needs several seconds to be in its ‘Ready’ state before the DMD_Funtion() is ran or else the PULM() will be waiting for triggers and basically never exit. I noticed that even with the Pause(5) or even 10 that I put in before the the DMD funtion is ran, all that happens is that both workers wait 5 seconds before running simultaneously instead of being delayed like they are supposed to. How do I delay the exectution of the DMD funtion? is it possible that i = 3 is running before i = 2 causing both of interations to pause before executing? Are there any other ways I could accomplish parallel processing in this way without the use of 2 MATLAB windows?
Initially I thought using 2 push buttons on a GUI would work because I believed that push buttons worked in parallel, but I soon realized they were executed sequentially. So I now have to use parallel toolbox functions so that one push button with execute everything in parallel.
Here’s the code for the parfor:
parfor i=2:3
if i==2
%pause(1);
%DMD_Function(patternNumber,AvergeNumber,SamplingRate,BinningNumber=768/SamplingRate) 768/32
PULM(PonitNumber,NumberofRecording,AvergeNumber);
%disp(‘run 1’);
disp(‘done 1’)
else
pause(5);
DMD_Function(patternNumber,AvergeNumber,SamplingRate,BinningNumber);
disp(‘done 2’)
end
end
Note: ‘done 1’ does not display meaning it does not complete the MEX function but ‘All records were saved to file’ does display which is right before ‘Exit Gracefully’ for the Digitizer.
In a separate code I tried to why the pause was just making them both wait 5 seconds then simultaneously running instead of being delayed. Threadpools seem to do what I want, it executes i==2 and delays i == 3. I am still not sure as to why Processespools doesn’t do the same thing. I even tried to stop the execution of the i==3 line by having it check if i == 2 wrote in a file, but it just ccomes up with an error. this is the reason why I was wondering if i==3 is being executed faster than i==2.
stateFile = ‘state.txt’;
parfor i = 2:3
if i == 2
%pause(5);
disp(‘hi’)
fid = fopen(stateFile, ‘w’);
fwrite(fid, ‘5’, ‘char’);
fclose(fid);
else
pause(5);
if isfile(stateFile)
fid = fopen(stateFile, ‘r’);
state = fread(fid, ‘*char’)’;
fclose(fid);
% Check if the state value matches
if strcmp(state, ‘5’)
disp(‘hello’);
end
end
end
end parallel computing, parallel computing toolbox, parfor, data acquisition, digitizer, help, dmd, parallel pools MATLAB Answers — New Questions
​
Need a Matlab code
Sir I am doing M Sc project based on mixed finite element method i need help in solving the numerical problems which was done by matlabSir I am doing M Sc project based on mixed finite element method i need help in solving the numerical problems which was done by matlab Sir I am doing M Sc project based on mixed finite element method i need help in solving the numerical problems which was done by matlab numerical method, finite element analysis MATLAB Answers — New Questions
​
How can i draw shaded confidence region with nlparci and lsqcurvfit?
[k,Rsdnrm,Rsd,ExFlg,OptmInfo,Lmda,Jmat] = lsqcurvefit(@simulatedhs,k0,tforward,[Hdata,HSdata],lb,ub);
CI = nlparci(k,Rsd,’jacobian’,Jmat);[k,Rsdnrm,Rsd,ExFlg,OptmInfo,Lmda,Jmat] = lsqcurvefit(@simulatedhs,k0,tforward,[Hdata,HSdata],lb,ub);
CI = nlparci(k,Rsd,’jacobian’,Jmat);Â [k,Rsdnrm,Rsd,ExFlg,OptmInfo,Lmda,Jmat] = lsqcurvefit(@simulatedhs,k0,tforward,[Hdata,HSdata],lb,ub);
CI = nlparci(k,Rsd,’jacobian’,Jmat); curve fitting, parameter estimation, statistics information, confidence region MATLAB Answers — New Questions
​
chosing the right Vnom, Qnom point in Shepherd model
Hello, I’m currently using the battery library in Simulink, based on the Shepherd model. When I input the nominal voltage values as specified in the datasheet, I notice that the curve accuracy diminishes after the nominal voltage range. However, when I manually select different points, I obtain a more accurate curve. Which approach is preferable: relying on the nominal voltage values provided in the datasheet or selecting my own points to achieve a more accurate representation of the battery’s behavior?Hello, I’m currently using the battery library in Simulink, based on the Shepherd model. When I input the nominal voltage values as specified in the datasheet, I notice that the curve accuracy diminishes after the nominal voltage range. However, when I manually select different points, I obtain a more accurate curve. Which approach is preferable: relying on the nominal voltage values provided in the datasheet or selecting my own points to achieve a more accurate representation of the battery’s behavior? Hello, I’m currently using the battery library in Simulink, based on the Shepherd model. When I input the nominal voltage values as specified in the datasheet, I notice that the curve accuracy diminishes after the nominal voltage range. However, when I manually select different points, I obtain a more accurate curve. Which approach is preferable: relying on the nominal voltage values provided in the datasheet or selecting my own points to achieve a more accurate representation of the battery’s behavior? matlab, plot, battery_system_management, shepherd MATLAB Answers — New Questions
​
How to run standalone application from the folder where it is located?
I made a standalone app using matlab app designer. While making package, the default installation folder for package is showing as ProgramFiles or AppData. But I want to run from the local folder only where the app is located in the User PC. Please suggest the way for doing this.I made a standalone app using matlab app designer. While making package, the default installation folder for package is showing as ProgramFiles or AppData. But I want to run from the local folder only where the app is located in the User PC. Please suggest the way for doing this. I made a standalone app using matlab app designer. While making package, the default installation folder for package is showing as ProgramFiles or AppData. But I want to run from the local folder only where the app is located in the User PC. Please suggest the way for doing this. standalone application MATLAB Answers — New Questions
​
Can I please get an accessibility statement
Yes I am trying to find your accessibility statement in case I have a student with disabilities in my class thank youYes I am trying to find your accessibility statement in case I have a student with disabilities in my class thank you Yes I am trying to find your accessibility statement in case I have a student with disabilities in my class thank you accessibility MATLAB Answers — New Questions
​
How to use OutputFcn with fitnlm
Hi all,
How do I execute an OutputFcn function in each iteration inside fitlm? I have tried to run the following code but it does not print anything or throw an exception.
load carbig
tbl = table(Horsepower,Weight,MPG);
modelfun = @(b,x)b(1) + b(2)*x(:,1).^b(3) + …
b(4)*x(:,2).^b(5);
beta0 = [-50 500 -1 500 -1];
opts = statset("fitnlm");
opts.OutputFcn = @(x)disp(x);
mdl = fitnlm(tbl,modelfun,beta0,"Options",opts);Hi all,
How do I execute an OutputFcn function in each iteration inside fitlm? I have tried to run the following code but it does not print anything or throw an exception.
load carbig
tbl = table(Horsepower,Weight,MPG);
modelfun = @(b,x)b(1) + b(2)*x(:,1).^b(3) + …
b(4)*x(:,2).^b(5);
beta0 = [-50 500 -1 500 -1];
opts = statset("fitnlm");
opts.OutputFcn = @(x)disp(x);
mdl = fitnlm(tbl,modelfun,beta0,"Options",opts);Â Hi all,
How do I execute an OutputFcn function in each iteration inside fitlm? I have tried to run the following code but it does not print anything or throw an exception.
load carbig
tbl = table(Horsepower,Weight,MPG);
modelfun = @(b,x)b(1) + b(2)*x(:,1).^b(3) + …
b(4)*x(:,2).^b(5);
beta0 = [-50 500 -1 500 -1];
opts = statset("fitnlm");
opts.OutputFcn = @(x)disp(x);
mdl = fitnlm(tbl,modelfun,beta0,"Options",opts); fitnlm, outputfcn, fit nonlinear regression model MATLAB Answers — New Questions
​
Troubleshooting connection issues with Speedgoat target computers
How do I troubleshoot connection issues between my Windows or Linux host PC, and a Speedgoat target computer configured with Simulink Real-Time (SLRT)?How do I troubleshoot connection issues between my Windows or Linux host PC, and a Speedgoat target computer configured with Simulink Real-Time (SLRT)? How do I troubleshoot connection issues between my Windows or Linux host PC, and a Speedgoat target computer configured with Simulink Real-Time (SLRT)? speedgoat, connection, fail, firewall, ports, slrt MATLAB Answers — New Questions
​
Prevent changing values in GUI
Dear firends,
I have a querry reagrding the GUI formation in MATLAB. I have made a GUI and everything is ok but the values of tab inside the listerner called as ‘angle’ can be changed with edit filed and slider, which has limit obj.angle = [-10,10].
During static code given below also I have made the condtion ‘if (obj.angle ~= obj.angle) & obj.angle<10 & obj.angle>-10’. Hence the static code is not executed if the slider and editfield is out of range of given limit.
But the problem is if I go inside the angle.Value and write some number not in range or limit of angle then even though the static code is not excuted but the value inside angle.Value is updated and stored. I dont want this.
Hence, I request to please suggest some way by which I can prevent the value from being updated manually inside the angle.Value. And the angle.Value should have the same limit as it has when input is given through slider or edit field.
I request to please help.
Thanking all,
Kind regards
classdef classErowin < handle
properties
fig
serial_port
UI
angle
end
properties (SetObservable, AbortSet) % for listeners
angle
end
methods
function obj = class() % constructor
obj.angle = [-10,10]
edit_filed
slider
end
end
methods (Static)
function handlePropEvents(src,evnt)
obj = evnt.AffectedObject;
switch src.Name
case {‘angle’}
if (obj.angle ~= obj.angle) & obj.angle<10 & obj.angle>-10
end
end
end
end
endDear firends,
I have a querry reagrding the GUI formation in MATLAB. I have made a GUI and everything is ok but the values of tab inside the listerner called as ‘angle’ can be changed with edit filed and slider, which has limit obj.angle = [-10,10].
During static code given below also I have made the condtion ‘if (obj.angle ~= obj.angle) & obj.angle<10 & obj.angle>-10’. Hence the static code is not executed if the slider and editfield is out of range of given limit.
But the problem is if I go inside the angle.Value and write some number not in range or limit of angle then even though the static code is not excuted but the value inside angle.Value is updated and stored. I dont want this.
Hence, I request to please suggest some way by which I can prevent the value from being updated manually inside the angle.Value. And the angle.Value should have the same limit as it has when input is given through slider or edit field.
I request to please help.
Thanking all,
Kind regards
classdef classErowin < handle
properties
fig
serial_port
UI
angle
end
properties (SetObservable, AbortSet) % for listeners
angle
end
methods
function obj = class() % constructor
obj.angle = [-10,10]
edit_filed
slider
end
end
methods (Static)
function handlePropEvents(src,evnt)
obj = evnt.AffectedObject;
switch src.Name
case {‘angle’}
if (obj.angle ~= obj.angle) & obj.angle<10 & obj.angle>-10
end
end
end
end
end Dear firends,
I have a querry reagrding the GUI formation in MATLAB. I have made a GUI and everything is ok but the values of tab inside the listerner called as ‘angle’ can be changed with edit filed and slider, which has limit obj.angle = [-10,10].
During static code given below also I have made the condtion ‘if (obj.angle ~= obj.angle) & obj.angle<10 & obj.angle>-10’. Hence the static code is not executed if the slider and editfield is out of range of given limit.
But the problem is if I go inside the angle.Value and write some number not in range or limit of angle then even though the static code is not excuted but the value inside angle.Value is updated and stored. I dont want this.
Hence, I request to please suggest some way by which I can prevent the value from being updated manually inside the angle.Value. And the angle.Value should have the same limit as it has when input is given through slider or edit field.
I request to please help.
Thanking all,
Kind regards
classdef classErowin < handle
properties
fig
serial_port
UI
angle
end
properties (SetObservable, AbortSet) % for listeners
angle
end
methods
function obj = class() % constructor
obj.angle = [-10,10]
edit_filed
slider
end
end
methods (Static)
function handlePropEvents(src,evnt)
obj = evnt.AffectedObject;
switch src.Name
case {‘angle’}
if (obj.angle ~= obj.angle) & obj.angle<10 & obj.angle>-10
end
end
end
end
end gui, matlab gui MATLAB Answers — New Questions
​
Call a Python function inside a MATLAB loop
In a Matlab script, is there a way to call a Python function, in loop for, in such a way that at every iteration the inputs of the Python function are different?
This is my case, where the arrays "a" and "b" are always different, and they return, obviously, different output:
% My Python function
import numpy as np
from scipy import stats
a = [7, 42, 61, 81, 115, 137, 80, 100, 121, 140, 127, 110, 81, 39, 59, 45, 38, 32, 29, 27, 35, 25, 22, 20, 19, 14, 12, 9, 8, 6, 3, 2, 2, 0, 0, 1, 0, 1, 0, 0];
b = a;
rng = np.random.default_rng()
method = stats.PermutationMethod(n_resamples=9999, random_state=rng)
res = stats.anderson_ksamp([a,b], method=method)
print(res.statistic)
print(res.critical_values)
print(res.pvalue)
To add more details, I would like to have something like this in Matlab:
% Call a Python function inside a MATLAB loop
for i = 1 : 10
a = randi([1 100],1,50);
b = randi([1 100],1,50);
out = call_python_function_here;
endIn a Matlab script, is there a way to call a Python function, in loop for, in such a way that at every iteration the inputs of the Python function are different?
This is my case, where the arrays "a" and "b" are always different, and they return, obviously, different output:
% My Python function
import numpy as np
from scipy import stats
a = [7, 42, 61, 81, 115, 137, 80, 100, 121, 140, 127, 110, 81, 39, 59, 45, 38, 32, 29, 27, 35, 25, 22, 20, 19, 14, 12, 9, 8, 6, 3, 2, 2, 0, 0, 1, 0, 1, 0, 0];
b = a;
rng = np.random.default_rng()
method = stats.PermutationMethod(n_resamples=9999, random_state=rng)
res = stats.anderson_ksamp([a,b], method=method)
print(res.statistic)
print(res.critical_values)
print(res.pvalue)
To add more details, I would like to have something like this in Matlab:
% Call a Python function inside a MATLAB loop
for i = 1 : 10
a = randi([1 100],1,50);
b = randi([1 100],1,50);
out = call_python_function_here;
end In a Matlab script, is there a way to call a Python function, in loop for, in such a way that at every iteration the inputs of the Python function are different?
This is my case, where the arrays "a" and "b" are always different, and they return, obviously, different output:
% My Python function
import numpy as np
from scipy import stats
a = [7, 42, 61, 81, 115, 137, 80, 100, 121, 140, 127, 110, 81, 39, 59, 45, 38, 32, 29, 27, 35, 25, 22, 20, 19, 14, 12, 9, 8, 6, 3, 2, 2, 0, 0, 1, 0, 1, 0, 0];
b = a;
rng = np.random.default_rng()
method = stats.PermutationMethod(n_resamples=9999, random_state=rng)
res = stats.anderson_ksamp([a,b], method=method)
print(res.statistic)
print(res.critical_values)
print(res.pvalue)
To add more details, I would like to have something like this in Matlab:
% Call a Python function inside a MATLAB loop
for i = 1 : 10
a = randi([1 100],1,50);
b = randi([1 100],1,50);
out = call_python_function_here;
end call python MATLAB Answers — New Questions
​
Does MATLAB run on Windows XP?
Does MATLAB run on Windows XP?Does MATLAB run on Windows XP? Does MATLAB run on Windows XP?  MATLAB Answers — New Questions
​
Jacobian calculation of symbolic variables which are function of other variables.
Hi everyone,
I am trying to find the jacobian for a transformation matrix. I am using symbolic variables (T, l, m, n). Each of these variables are function of 4 others variables (delta1 delta2 delta3 and delta4), as:
syms u v w p q r phi theta psi x y z;
syms delta1 delta2 delta3 delta4;
% Aerodynamics
V = sqrt(u^2 + v^2 + w^2);
q_bar = (1/2) * rho * V^2;
m = (-1.35* Kf * delta1^2) + (1.35* Kf * delta2^2) + (1.35*K * Kf * delta3^2) + (-1.35* Kf * delta4^2);
l = (0.904* Kf *delta1^2) + (-0.904* Kf *delta2^2) + (0.904* Kf *delta3^2) + (-0.904* Kf *delta4^2);
n = (Km * delta1^2) + (Km * delta2^2) – (Km * delta3^2) – (Km * delta4^2);
T1= (Kf * delta1^2);
T2= (Kf * delta2^2);
T3= (Kf * delta3^2);
T4= (Kf * delta4^2);
T= T1 + T2 + T3 + T4;
phi_dot = p + tan(theta) * (q * sin(phi) + r * cos(phi));
theta_dot = q * cos(phi) – r * sin(phi);
psi_dot = (q * sin(phi) + r * cos(phi)) / cos(theta);
x_dot = cos(psi)*cos(theta)*u + (cos(psi)*sin(theta)*sin(phi) – sin(psi)*cos(phi))*v + (cos(psi)*sin(theta)*cos(phi) + sin(psi)*sin(phi))*w;
y_dot = (sin(psi)*cos(theta))*u + (sin(psi)*sin(theta)*sin(phi) + cos(psi)*cos(phi))*v + (sin(psi)*sin(theta)*cos(phi) – cos(psi)*sin(phi))*w;
z_dot = -sin(theta)*u + cos(theta)*sin(phi)*v + cos(theta)*cos(phi)*w;
f_x = – mass*g * sin(theta);
f_y = mass*g * sin(phi) * cos(theta);
f_z = mass*g * cos(phi) * cos(theta) – T ;
u_dot = r*v – q*w + (1/mass) * (f_x);
v_dot = p*w – r*u + (1/mass) * (f_y);
w_dot = q*u – p*v + (1/mass) * (f_z);
p_dot = gam(1)*p*q – gam(2)*q*r + gam(3)*l + gam(4)*n;
q_dot = gam(5)*p*r – gam(6)*(p^2 – r^2) + (1/J_yy) * m;
r_dot = gam(7)*p*q – gam(1)*q*r + gam(4)*l + gam(8)*n;
% Collect dynamics
f = [ x_dot;
y_dot;
z_dot;
phi_dot;
theta_dot;
psi_dot;
u_dot;
v_dot;
w_dot;
p_dot;
q_dot;
r_dot];
jacobian(f,[T l m n]);
So when calculating jacobian(f,[T l m n]) , i have the error:
"Invalid argument at position 2. Argument must be a variable, a symfun without a formula, or a symfun whose formula is a variable."
Can someone please give me a solution to the problem ?Hi everyone,
I am trying to find the jacobian for a transformation matrix. I am using symbolic variables (T, l, m, n). Each of these variables are function of 4 others variables (delta1 delta2 delta3 and delta4), as:
syms u v w p q r phi theta psi x y z;
syms delta1 delta2 delta3 delta4;
% Aerodynamics
V = sqrt(u^2 + v^2 + w^2);
q_bar = (1/2) * rho * V^2;
m = (-1.35* Kf * delta1^2) + (1.35* Kf * delta2^2) + (1.35*K * Kf * delta3^2) + (-1.35* Kf * delta4^2);
l = (0.904* Kf *delta1^2) + (-0.904* Kf *delta2^2) + (0.904* Kf *delta3^2) + (-0.904* Kf *delta4^2);
n = (Km * delta1^2) + (Km * delta2^2) – (Km * delta3^2) – (Km * delta4^2);
T1= (Kf * delta1^2);
T2= (Kf * delta2^2);
T3= (Kf * delta3^2);
T4= (Kf * delta4^2);
T= T1 + T2 + T3 + T4;
phi_dot = p + tan(theta) * (q * sin(phi) + r * cos(phi));
theta_dot = q * cos(phi) – r * sin(phi);
psi_dot = (q * sin(phi) + r * cos(phi)) / cos(theta);
x_dot = cos(psi)*cos(theta)*u + (cos(psi)*sin(theta)*sin(phi) – sin(psi)*cos(phi))*v + (cos(psi)*sin(theta)*cos(phi) + sin(psi)*sin(phi))*w;
y_dot = (sin(psi)*cos(theta))*u + (sin(psi)*sin(theta)*sin(phi) + cos(psi)*cos(phi))*v + (sin(psi)*sin(theta)*cos(phi) – cos(psi)*sin(phi))*w;
z_dot = -sin(theta)*u + cos(theta)*sin(phi)*v + cos(theta)*cos(phi)*w;
f_x = – mass*g * sin(theta);
f_y = mass*g * sin(phi) * cos(theta);
f_z = mass*g * cos(phi) * cos(theta) – T ;
u_dot = r*v – q*w + (1/mass) * (f_x);
v_dot = p*w – r*u + (1/mass) * (f_y);
w_dot = q*u – p*v + (1/mass) * (f_z);
p_dot = gam(1)*p*q – gam(2)*q*r + gam(3)*l + gam(4)*n;
q_dot = gam(5)*p*r – gam(6)*(p^2 – r^2) + (1/J_yy) * m;
r_dot = gam(7)*p*q – gam(1)*q*r + gam(4)*l + gam(8)*n;
% Collect dynamics
f = [ x_dot;
y_dot;
z_dot;
phi_dot;
theta_dot;
psi_dot;
u_dot;
v_dot;
w_dot;
p_dot;
q_dot;
r_dot];
jacobian(f,[T l m n]);
So when calculating jacobian(f,[T l m n]) , i have the error:
"Invalid argument at position 2. Argument must be a variable, a symfun without a formula, or a symfun whose formula is a variable."
Can someone please give me a solution to the problem ? Hi everyone,
I am trying to find the jacobian for a transformation matrix. I am using symbolic variables (T, l, m, n). Each of these variables are function of 4 others variables (delta1 delta2 delta3 and delta4), as:
syms u v w p q r phi theta psi x y z;
syms delta1 delta2 delta3 delta4;
% Aerodynamics
V = sqrt(u^2 + v^2 + w^2);
q_bar = (1/2) * rho * V^2;
m = (-1.35* Kf * delta1^2) + (1.35* Kf * delta2^2) + (1.35*K * Kf * delta3^2) + (-1.35* Kf * delta4^2);
l = (0.904* Kf *delta1^2) + (-0.904* Kf *delta2^2) + (0.904* Kf *delta3^2) + (-0.904* Kf *delta4^2);
n = (Km * delta1^2) + (Km * delta2^2) – (Km * delta3^2) – (Km * delta4^2);
T1= (Kf * delta1^2);
T2= (Kf * delta2^2);
T3= (Kf * delta3^2);
T4= (Kf * delta4^2);
T= T1 + T2 + T3 + T4;
phi_dot = p + tan(theta) * (q * sin(phi) + r * cos(phi));
theta_dot = q * cos(phi) – r * sin(phi);
psi_dot = (q * sin(phi) + r * cos(phi)) / cos(theta);
x_dot = cos(psi)*cos(theta)*u + (cos(psi)*sin(theta)*sin(phi) – sin(psi)*cos(phi))*v + (cos(psi)*sin(theta)*cos(phi) + sin(psi)*sin(phi))*w;
y_dot = (sin(psi)*cos(theta))*u + (sin(psi)*sin(theta)*sin(phi) + cos(psi)*cos(phi))*v + (sin(psi)*sin(theta)*cos(phi) – cos(psi)*sin(phi))*w;
z_dot = -sin(theta)*u + cos(theta)*sin(phi)*v + cos(theta)*cos(phi)*w;
f_x = – mass*g * sin(theta);
f_y = mass*g * sin(phi) * cos(theta);
f_z = mass*g * cos(phi) * cos(theta) – T ;
u_dot = r*v – q*w + (1/mass) * (f_x);
v_dot = p*w – r*u + (1/mass) * (f_y);
w_dot = q*u – p*v + (1/mass) * (f_z);
p_dot = gam(1)*p*q – gam(2)*q*r + gam(3)*l + gam(4)*n;
q_dot = gam(5)*p*r – gam(6)*(p^2 – r^2) + (1/J_yy) * m;
r_dot = gam(7)*p*q – gam(1)*q*r + gam(4)*l + gam(8)*n;
% Collect dynamics
f = [ x_dot;
y_dot;
z_dot;
phi_dot;
theta_dot;
psi_dot;
u_dot;
v_dot;
w_dot;
p_dot;
q_dot;
r_dot];
jacobian(f,[T l m n]);
So when calculating jacobian(f,[T l m n]) , i have the error:
"Invalid argument at position 2. Argument must be a variable, a symfun without a formula, or a symfun whose formula is a variable."
Can someone please give me a solution to the problem ? jacobian MATLAB Answers — New Questions
​
multiplying a function handle by a constant
For the code below I am trying to find the polynomial equation that represents the system. There are 4-2nd ODE equations I have made them into 4-1st ODE the first order differentials become "y1,y2,y3and y4" and the 2nd order differentials become a first order. I then put all 4 into a matlabFunction, how do I multiple the function handle by the constant"h" with out getting the error "Operator ‘*’ is not supported for operands of type ‘function_handle’."?
The rest of the code works if working with a single differential equation fx(x,y,t) with only "x,y,t" but in my equations I have "Xf,Xr,Xb,theta" and "Vf,Vr,Vb,omega" that I have chosen to represent as "x1,x2,x3,x4" and "y1,y2,y3,y4" respectively. The next question is will I run into problems here as well?
I am not that expirenced working with matlabFunction command to know how to get this to work. The project requires me to use 2 different numerical methods to find the polynomial equation that best fits so I cannot use "Polyfit" to get the polynomial that best fits. Any suggestions that can help me to get this to work would be appreciated.
clear,close,clc
%______________________________________________________________SOLUITION_2_Heun’s_Method_(for second order differential equations)_&__Least-Square_Nethod____________________________________________________________%
%4 Equations representing the system working with
% MfXf"=Ksf([Xb-(L1*theta)]-Xf)+Bsf([Xb’-(L1*theta’)-Xf’)-(Kf*Xf);
% MrXr"=Ksr([Xb+(L2*theta)]-Xr)+Bsr([Xb’+(L2*theta’)]-Xr’)-(Kr*Xr) ;
% MbXb"=Ksf([Xb-(L1*theta)]-Xf)+Bsf([Xb’-(L1*theta’)]-Xf’)+Ksr([Xb+(L2*theta)]-Xr)+Bsr([Xb’+(L2*theta’)]-Xr’)+fa(t);
% Ic*theta"={-[Ksf(Xf-(L1*theta))*L1]-[Bsf(Xf’-(L1*theta’))*L1]+[Ksr(Xr+(L2*theta))*L2]+[Bsr(Xr’+(L2*theta’))*L2]+[fa(t)*L3]};
clc
clear
%————————————————-SYSTEM_PARAMETERS———————————————————————————————————————————————————-
Ic=1356; %kg-m^2
Mb=730; %kg
Mf=59; %kg
Mr=45; %kg
Kf=23000; %N/m
Ksf=18750; %N/m
Kr=16182; %N/m
Ksr=12574; %N/m
Bsf=100; %N*s/m
Bsr=100; %N*s/m
L1=1.45; %m
L2=1.39; %m
L3=0.67; %m
t=[0:20]; % time from 0 to 20 seconds
n=4; %order of the polynomial
%Initial Conditions-system at rest therefore x(0)=0 dXf/dt(0)=0 dXr/dt(0)=0 dXb/dt(0)=0 dtheta/dt(0)=0 ;
%time from 0 to 20 h=dx=5;
x0=0; %x at initial condition
y0=0; %y at initial condition
t0=0; %t at the start
dx=5; %delta(x) or h
h=dx;
tm=20; %what value of (x) you are ending at
Xf = sym(‘x1’); %x1=Xf
Xr = sym(‘x2’); %x2=Xr
Xb = sym(‘x3’); %x3=Xb
theta = sym(‘x4’); %x4=theta
Vf = sym(‘y1′); %y1=Xf’ = Vf = dXf/dt = Xf_1
Vr = sym(‘y2′); %y2=Xr’ = Vr = dXr/dt = Xr_1
Vb = sym(‘y3′); %y3=Xb’ = Vb = dXb/dt = Xb_1
omega = sym(‘y4’); %y4=theta’= omega = dtheta/dt = theta_1
t = sym(‘t’);
% MfXf"=Ksf([Xb-(L1*theta)]-Xf)+Bsf([Xb’-(L1*theta’)-Xf’)-(Kf*Xf);
% Vf’=(1/Mf)(Ksf([Xb-(L1*theta)]-Xf)+Bsf([Xb’-(L1*theta’)-Xf’)-(Kf*Xf));
% MrXr"=Ksr([Xb+(L2*theta)]-Xr)+Bsr([Xb’+(L2*theta’)]-Xr’)-(Kr*Xr) ;
% Vr’=(1/Mr)(Ksr([Xb+(L2*theta)]-Xr)+Bsr([Xb’+(L2*theta’)]-Xr’)-(Kr*Xr)) ;
% MbXb"=Ksf([Xb-(L1*theta)]-Xf)+Bsf([Xb’-(L1*theta’)]-Xf’)+Ksr([Xb+(L2*theta)]-Xr)+Bsr([Xb’+(L2*theta’)]-Xr’)+fa(t);
% Vb’=(1/Mb)(-Ksf([Xb-(L1*theta)]-Xf)-Bsf([Xb’-(L1*theta’)]-Xf’)-Ksr([Xb+(L2*theta)]-Xr)-Bsr([Xb’+(L2*theta’)]-Xr’)+fa(t));
% Ic*theta"={-[Ksf(Xf-(L1*theta))*L1]-[Bsf(Xf’-(L1*theta’))*L1]+[Ksr(Xr+(L2*theta))*L2]+[Bsr(Xr’+(L2*theta’))*L2]+[fa(t)*L3]};
% omega’=(1/Ic){[-Ksf*Xf + Ksf((L1)^2)*theta)] + [-Bsf*Vf*L1 + Bsf*Vf*L1 + Bsf((L1)^2)*omega)] + [Ksr*Xr*L2 + Ksr((L2)^2)*theta)] + [Bsr*Vr*L2 + Bsr((L2)^2)*omega)*L2] + [fa(t)*L3]};
Xf_2=(Ksf*((Xb-(L1*theta))-Xf)+Bsf*((Vb-(L1*omega)-Vf)-(Kf*Xf)))/Mf;
Xr_2=(Ksr*((Xb+(L2*theta))-Xr)+Bsr*((Vb+(L2*omega))-Vr)-(Kr*Xr))/Mr;
Xb_2=(Ksf*((Xb-(L1*theta))-Xf)+Bsf*((Vb-(L1*theta’))-Vf)+Ksr*((Xb+(L2*theta))-Xr)+Bsr*((Vb+(L2*omega))-Vr)+(10*exp(-(5*t))))/Mb;
theta_2=((-(Ksf*(Xf-(L1^2*theta))*L1)-(Bsf*(Vf-(L1*omega))*L1)+(Ksr*(Xr+(L2*theta))*L2)+(Bsr*(Vr+(L2*omega))*L2)+((10*exp(-(5*t)))*L3)))/Ic;
Eqns=[Xf_2; Xr_2; Xb_2; theta_2];
F1=matlabFunction(Eqns,’Vars’,{‘x1′,’x2′,’x3′,’x4′,’y1′,’y2′,’y3′,’y4′,’t’})
%==INPUT SECTION for Euler’s and Heun’s==%
fx=@(x,y,t)y;
fy=@(x,y,t)F1;
%==CALCULATIONS SECTION==%
tn=t0:h:tm;
xn(1) = x0;
yn(1) = y0;
for i=1:length(tn)
%==EULER’S METHOD
xn(i+1)=xn(i)+fx(xn(i),yn(i),tn(i))*h;
yn(i+1)=yn(i)+fy(xn(i),yn(i),tn(i))*h;
%==NEXT 3 LINES ARE FOR HEUN’S METHOD
tn(i+1)=tn(i)+h;
xn(i+1)=xn(i)+0.5*(fx(xn(i),yn(i),tn(i))+fx(xn(i+1),yn(i+1),tn(i+1)))*h;
yn(i+1)=yn(i)+0.5*(fy(xn(i),yn(i),tn(i))+fy(xn(i+1),yn(i+1),tn(i+1)))*h;
fprintf(‘t=%0.2ft x=%0.3ft y=%0.3fn’,tn(i),xn(i),yn(i))
end
%%%LEAST SQUARE METHOD-FINDS POLYNOMIAL FOR GIVEN DATA SET%%%%%
%INPUT SECTION for Least-Square
X=xn;
Y=yn;
%%__CALCULATIONS SECTION__%%
k=length(X); %NUMBER OF AVAILABLE DATA POINTS
m=n+1; %SIZE OF THE COEFFICENT MATRIX
A=zeros(m,m); %COEFFICENT MATRIX
for j=1:m
for i=1:m
A(j,i)=sum(X.^(i+j-2));
end
end
B=zeros(m,1); %FORCING FUNCTION VECTOR
for i=1:m;
B(i)=sum(Y.*X.^(i-1));
end
a1=AB %COEFFICIENTS FOR THE POLYNOMINAL–> y=a0+a1*x+a2*x^2….an*x^n CAN BE REPLACED BY GAUSSIAN ELIMINATION
%%%%%=========GAUSSIAN ELIMINATION TO FIND "a"========%%%%%%
%%%INPUT SECTION
%CALCULATION SECTION
AB=[A B]; %Augumentent matrix
R=size(AB,1); %# OF ROWS IN AB
C=size(AB,2); %# OF COLUMNS IN AB
%%%%FOWARD ELIMINATION SECTION
for J=1:R-1
[M,I]=max(abs(AB(J:R,J))); %M=MAXIMUM VALUE, I=LOCATION OF THE MAXIMUM VALUE IN THE 1ST ROW
temp=AB(J,:);
AB(J,:)=AB(I+(J-1),:);
AB(I+(J-1),:)=temp;
for i=(J+1):R;
if AB(i,J)~=0;
AB(i,:)=AB(i,:)-(AB(i,J)/AB(J,J))*AB(J,:);
end
end
end
%%%%BACKWARDS SUBSTITUTION
a(R)=AB(R,C)/AB(R,R);
for i=R-1:-1:1
a(i)=(AB(i,C)-AB(i,i+1:R)*a(i+1:R)’)/AB(i,i);
end
disp(a)
syms X
P=0;
for i=1:m;
TT=a(i)*X^(i-1); %T=INDIVIDUAL POLYNOMIAL TERMS
P=P+TT;
end
display(P)
%========END OF GAUSSIAN ELIMINATION=======%%%%%%%%For the code below I am trying to find the polynomial equation that represents the system. There are 4-2nd ODE equations I have made them into 4-1st ODE the first order differentials become "y1,y2,y3and y4" and the 2nd order differentials become a first order. I then put all 4 into a matlabFunction, how do I multiple the function handle by the constant"h" with out getting the error "Operator ‘*’ is not supported for operands of type ‘function_handle’."?
The rest of the code works if working with a single differential equation fx(x,y,t) with only "x,y,t" but in my equations I have "Xf,Xr,Xb,theta" and "Vf,Vr,Vb,omega" that I have chosen to represent as "x1,x2,x3,x4" and "y1,y2,y3,y4" respectively. The next question is will I run into problems here as well?
I am not that expirenced working with matlabFunction command to know how to get this to work. The project requires me to use 2 different numerical methods to find the polynomial equation that best fits so I cannot use "Polyfit" to get the polynomial that best fits. Any suggestions that can help me to get this to work would be appreciated.
clear,close,clc
%______________________________________________________________SOLUITION_2_Heun’s_Method_(for second order differential equations)_&__Least-Square_Nethod____________________________________________________________%
%4 Equations representing the system working with
% MfXf"=Ksf([Xb-(L1*theta)]-Xf)+Bsf([Xb’-(L1*theta’)-Xf’)-(Kf*Xf);
% MrXr"=Ksr([Xb+(L2*theta)]-Xr)+Bsr([Xb’+(L2*theta’)]-Xr’)-(Kr*Xr) ;
% MbXb"=Ksf([Xb-(L1*theta)]-Xf)+Bsf([Xb’-(L1*theta’)]-Xf’)+Ksr([Xb+(L2*theta)]-Xr)+Bsr([Xb’+(L2*theta’)]-Xr’)+fa(t);
% Ic*theta"={-[Ksf(Xf-(L1*theta))*L1]-[Bsf(Xf’-(L1*theta’))*L1]+[Ksr(Xr+(L2*theta))*L2]+[Bsr(Xr’+(L2*theta’))*L2]+[fa(t)*L3]};
clc
clear
%————————————————-SYSTEM_PARAMETERS———————————————————————————————————————————————————-
Ic=1356; %kg-m^2
Mb=730; %kg
Mf=59; %kg
Mr=45; %kg
Kf=23000; %N/m
Ksf=18750; %N/m
Kr=16182; %N/m
Ksr=12574; %N/m
Bsf=100; %N*s/m
Bsr=100; %N*s/m
L1=1.45; %m
L2=1.39; %m
L3=0.67; %m
t=[0:20]; % time from 0 to 20 seconds
n=4; %order of the polynomial
%Initial Conditions-system at rest therefore x(0)=0 dXf/dt(0)=0 dXr/dt(0)=0 dXb/dt(0)=0 dtheta/dt(0)=0 ;
%time from 0 to 20 h=dx=5;
x0=0; %x at initial condition
y0=0; %y at initial condition
t0=0; %t at the start
dx=5; %delta(x) or h
h=dx;
tm=20; %what value of (x) you are ending at
Xf = sym(‘x1’); %x1=Xf
Xr = sym(‘x2’); %x2=Xr
Xb = sym(‘x3’); %x3=Xb
theta = sym(‘x4’); %x4=theta
Vf = sym(‘y1′); %y1=Xf’ = Vf = dXf/dt = Xf_1
Vr = sym(‘y2′); %y2=Xr’ = Vr = dXr/dt = Xr_1
Vb = sym(‘y3′); %y3=Xb’ = Vb = dXb/dt = Xb_1
omega = sym(‘y4’); %y4=theta’= omega = dtheta/dt = theta_1
t = sym(‘t’);
% MfXf"=Ksf([Xb-(L1*theta)]-Xf)+Bsf([Xb’-(L1*theta’)-Xf’)-(Kf*Xf);
% Vf’=(1/Mf)(Ksf([Xb-(L1*theta)]-Xf)+Bsf([Xb’-(L1*theta’)-Xf’)-(Kf*Xf));
% MrXr"=Ksr([Xb+(L2*theta)]-Xr)+Bsr([Xb’+(L2*theta’)]-Xr’)-(Kr*Xr) ;
% Vr’=(1/Mr)(Ksr([Xb+(L2*theta)]-Xr)+Bsr([Xb’+(L2*theta’)]-Xr’)-(Kr*Xr)) ;
% MbXb"=Ksf([Xb-(L1*theta)]-Xf)+Bsf([Xb’-(L1*theta’)]-Xf’)+Ksr([Xb+(L2*theta)]-Xr)+Bsr([Xb’+(L2*theta’)]-Xr’)+fa(t);
% Vb’=(1/Mb)(-Ksf([Xb-(L1*theta)]-Xf)-Bsf([Xb’-(L1*theta’)]-Xf’)-Ksr([Xb+(L2*theta)]-Xr)-Bsr([Xb’+(L2*theta’)]-Xr’)+fa(t));
% Ic*theta"={-[Ksf(Xf-(L1*theta))*L1]-[Bsf(Xf’-(L1*theta’))*L1]+[Ksr(Xr+(L2*theta))*L2]+[Bsr(Xr’+(L2*theta’))*L2]+[fa(t)*L3]};
% omega’=(1/Ic){[-Ksf*Xf + Ksf((L1)^2)*theta)] + [-Bsf*Vf*L1 + Bsf*Vf*L1 + Bsf((L1)^2)*omega)] + [Ksr*Xr*L2 + Ksr((L2)^2)*theta)] + [Bsr*Vr*L2 + Bsr((L2)^2)*omega)*L2] + [fa(t)*L3]};
Xf_2=(Ksf*((Xb-(L1*theta))-Xf)+Bsf*((Vb-(L1*omega)-Vf)-(Kf*Xf)))/Mf;
Xr_2=(Ksr*((Xb+(L2*theta))-Xr)+Bsr*((Vb+(L2*omega))-Vr)-(Kr*Xr))/Mr;
Xb_2=(Ksf*((Xb-(L1*theta))-Xf)+Bsf*((Vb-(L1*theta’))-Vf)+Ksr*((Xb+(L2*theta))-Xr)+Bsr*((Vb+(L2*omega))-Vr)+(10*exp(-(5*t))))/Mb;
theta_2=((-(Ksf*(Xf-(L1^2*theta))*L1)-(Bsf*(Vf-(L1*omega))*L1)+(Ksr*(Xr+(L2*theta))*L2)+(Bsr*(Vr+(L2*omega))*L2)+((10*exp(-(5*t)))*L3)))/Ic;
Eqns=[Xf_2; Xr_2; Xb_2; theta_2];
F1=matlabFunction(Eqns,’Vars’,{‘x1′,’x2′,’x3′,’x4′,’y1′,’y2′,’y3′,’y4′,’t’})
%==INPUT SECTION for Euler’s and Heun’s==%
fx=@(x,y,t)y;
fy=@(x,y,t)F1;
%==CALCULATIONS SECTION==%
tn=t0:h:tm;
xn(1) = x0;
yn(1) = y0;
for i=1:length(tn)
%==EULER’S METHOD
xn(i+1)=xn(i)+fx(xn(i),yn(i),tn(i))*h;
yn(i+1)=yn(i)+fy(xn(i),yn(i),tn(i))*h;
%==NEXT 3 LINES ARE FOR HEUN’S METHOD
tn(i+1)=tn(i)+h;
xn(i+1)=xn(i)+0.5*(fx(xn(i),yn(i),tn(i))+fx(xn(i+1),yn(i+1),tn(i+1)))*h;
yn(i+1)=yn(i)+0.5*(fy(xn(i),yn(i),tn(i))+fy(xn(i+1),yn(i+1),tn(i+1)))*h;
fprintf(‘t=%0.2ft x=%0.3ft y=%0.3fn’,tn(i),xn(i),yn(i))
end
%%%LEAST SQUARE METHOD-FINDS POLYNOMIAL FOR GIVEN DATA SET%%%%%
%INPUT SECTION for Least-Square
X=xn;
Y=yn;
%%__CALCULATIONS SECTION__%%
k=length(X); %NUMBER OF AVAILABLE DATA POINTS
m=n+1; %SIZE OF THE COEFFICENT MATRIX
A=zeros(m,m); %COEFFICENT MATRIX
for j=1:m
for i=1:m
A(j,i)=sum(X.^(i+j-2));
end
end
B=zeros(m,1); %FORCING FUNCTION VECTOR
for i=1:m;
B(i)=sum(Y.*X.^(i-1));
end
a1=AB %COEFFICIENTS FOR THE POLYNOMINAL–> y=a0+a1*x+a2*x^2….an*x^n CAN BE REPLACED BY GAUSSIAN ELIMINATION
%%%%%=========GAUSSIAN ELIMINATION TO FIND "a"========%%%%%%
%%%INPUT SECTION
%CALCULATION SECTION
AB=[A B]; %Augumentent matrix
R=size(AB,1); %# OF ROWS IN AB
C=size(AB,2); %# OF COLUMNS IN AB
%%%%FOWARD ELIMINATION SECTION
for J=1:R-1
[M,I]=max(abs(AB(J:R,J))); %M=MAXIMUM VALUE, I=LOCATION OF THE MAXIMUM VALUE IN THE 1ST ROW
temp=AB(J,:);
AB(J,:)=AB(I+(J-1),:);
AB(I+(J-1),:)=temp;
for i=(J+1):R;
if AB(i,J)~=0;
AB(i,:)=AB(i,:)-(AB(i,J)/AB(J,J))*AB(J,:);
end
end
end
%%%%BACKWARDS SUBSTITUTION
a(R)=AB(R,C)/AB(R,R);
for i=R-1:-1:1
a(i)=(AB(i,C)-AB(i,i+1:R)*a(i+1:R)’)/AB(i,i);
end
disp(a)
syms X
P=0;
for i=1:m;
TT=a(i)*X^(i-1); %T=INDIVIDUAL POLYNOMIAL TERMS
P=P+TT;
end
display(P)
%========END OF GAUSSIAN ELIMINATION=======%%%%%%%%Â For the code below I am trying to find the polynomial equation that represents the system. There are 4-2nd ODE equations I have made them into 4-1st ODE the first order differentials become "y1,y2,y3and y4" and the 2nd order differentials become a first order. I then put all 4 into a matlabFunction, how do I multiple the function handle by the constant"h" with out getting the error "Operator ‘*’ is not supported for operands of type ‘function_handle’."?
The rest of the code works if working with a single differential equation fx(x,y,t) with only "x,y,t" but in my equations I have "Xf,Xr,Xb,theta" and "Vf,Vr,Vb,omega" that I have chosen to represent as "x1,x2,x3,x4" and "y1,y2,y3,y4" respectively. The next question is will I run into problems here as well?
I am not that expirenced working with matlabFunction command to know how to get this to work. The project requires me to use 2 different numerical methods to find the polynomial equation that best fits so I cannot use "Polyfit" to get the polynomial that best fits. Any suggestions that can help me to get this to work would be appreciated.
clear,close,clc
%______________________________________________________________SOLUITION_2_Heun’s_Method_(for second order differential equations)_&__Least-Square_Nethod____________________________________________________________%
%4 Equations representing the system working with
% MfXf"=Ksf([Xb-(L1*theta)]-Xf)+Bsf([Xb’-(L1*theta’)-Xf’)-(Kf*Xf);
% MrXr"=Ksr([Xb+(L2*theta)]-Xr)+Bsr([Xb’+(L2*theta’)]-Xr’)-(Kr*Xr) ;
% MbXb"=Ksf([Xb-(L1*theta)]-Xf)+Bsf([Xb’-(L1*theta’)]-Xf’)+Ksr([Xb+(L2*theta)]-Xr)+Bsr([Xb’+(L2*theta’)]-Xr’)+fa(t);
% Ic*theta"={-[Ksf(Xf-(L1*theta))*L1]-[Bsf(Xf’-(L1*theta’))*L1]+[Ksr(Xr+(L2*theta))*L2]+[Bsr(Xr’+(L2*theta’))*L2]+[fa(t)*L3]};
clc
clear
%————————————————-SYSTEM_PARAMETERS———————————————————————————————————————————————————-
Ic=1356; %kg-m^2
Mb=730; %kg
Mf=59; %kg
Mr=45; %kg
Kf=23000; %N/m
Ksf=18750; %N/m
Kr=16182; %N/m
Ksr=12574; %N/m
Bsf=100; %N*s/m
Bsr=100; %N*s/m
L1=1.45; %m
L2=1.39; %m
L3=0.67; %m
t=[0:20]; % time from 0 to 20 seconds
n=4; %order of the polynomial
%Initial Conditions-system at rest therefore x(0)=0 dXf/dt(0)=0 dXr/dt(0)=0 dXb/dt(0)=0 dtheta/dt(0)=0 ;
%time from 0 to 20 h=dx=5;
x0=0; %x at initial condition
y0=0; %y at initial condition
t0=0; %t at the start
dx=5; %delta(x) or h
h=dx;
tm=20; %what value of (x) you are ending at
Xf = sym(‘x1’); %x1=Xf
Xr = sym(‘x2’); %x2=Xr
Xb = sym(‘x3’); %x3=Xb
theta = sym(‘x4’); %x4=theta
Vf = sym(‘y1′); %y1=Xf’ = Vf = dXf/dt = Xf_1
Vr = sym(‘y2′); %y2=Xr’ = Vr = dXr/dt = Xr_1
Vb = sym(‘y3′); %y3=Xb’ = Vb = dXb/dt = Xb_1
omega = sym(‘y4’); %y4=theta’= omega = dtheta/dt = theta_1
t = sym(‘t’);
% MfXf"=Ksf([Xb-(L1*theta)]-Xf)+Bsf([Xb’-(L1*theta’)-Xf’)-(Kf*Xf);
% Vf’=(1/Mf)(Ksf([Xb-(L1*theta)]-Xf)+Bsf([Xb’-(L1*theta’)-Xf’)-(Kf*Xf));
% MrXr"=Ksr([Xb+(L2*theta)]-Xr)+Bsr([Xb’+(L2*theta’)]-Xr’)-(Kr*Xr) ;
% Vr’=(1/Mr)(Ksr([Xb+(L2*theta)]-Xr)+Bsr([Xb’+(L2*theta’)]-Xr’)-(Kr*Xr)) ;
% MbXb"=Ksf([Xb-(L1*theta)]-Xf)+Bsf([Xb’-(L1*theta’)]-Xf’)+Ksr([Xb+(L2*theta)]-Xr)+Bsr([Xb’+(L2*theta’)]-Xr’)+fa(t);
% Vb’=(1/Mb)(-Ksf([Xb-(L1*theta)]-Xf)-Bsf([Xb’-(L1*theta’)]-Xf’)-Ksr([Xb+(L2*theta)]-Xr)-Bsr([Xb’+(L2*theta’)]-Xr’)+fa(t));
% Ic*theta"={-[Ksf(Xf-(L1*theta))*L1]-[Bsf(Xf’-(L1*theta’))*L1]+[Ksr(Xr+(L2*theta))*L2]+[Bsr(Xr’+(L2*theta’))*L2]+[fa(t)*L3]};
% omega’=(1/Ic){[-Ksf*Xf + Ksf((L1)^2)*theta)] + [-Bsf*Vf*L1 + Bsf*Vf*L1 + Bsf((L1)^2)*omega)] + [Ksr*Xr*L2 + Ksr((L2)^2)*theta)] + [Bsr*Vr*L2 + Bsr((L2)^2)*omega)*L2] + [fa(t)*L3]};
Xf_2=(Ksf*((Xb-(L1*theta))-Xf)+Bsf*((Vb-(L1*omega)-Vf)-(Kf*Xf)))/Mf;
Xr_2=(Ksr*((Xb+(L2*theta))-Xr)+Bsr*((Vb+(L2*omega))-Vr)-(Kr*Xr))/Mr;
Xb_2=(Ksf*((Xb-(L1*theta))-Xf)+Bsf*((Vb-(L1*theta’))-Vf)+Ksr*((Xb+(L2*theta))-Xr)+Bsr*((Vb+(L2*omega))-Vr)+(10*exp(-(5*t))))/Mb;
theta_2=((-(Ksf*(Xf-(L1^2*theta))*L1)-(Bsf*(Vf-(L1*omega))*L1)+(Ksr*(Xr+(L2*theta))*L2)+(Bsr*(Vr+(L2*omega))*L2)+((10*exp(-(5*t)))*L3)))/Ic;
Eqns=[Xf_2; Xr_2; Xb_2; theta_2];
F1=matlabFunction(Eqns,’Vars’,{‘x1′,’x2′,’x3′,’x4′,’y1′,’y2′,’y3′,’y4′,’t’})
%==INPUT SECTION for Euler’s and Heun’s==%
fx=@(x,y,t)y;
fy=@(x,y,t)F1;
%==CALCULATIONS SECTION==%
tn=t0:h:tm;
xn(1) = x0;
yn(1) = y0;
for i=1:length(tn)
%==EULER’S METHOD
xn(i+1)=xn(i)+fx(xn(i),yn(i),tn(i))*h;
yn(i+1)=yn(i)+fy(xn(i),yn(i),tn(i))*h;
%==NEXT 3 LINES ARE FOR HEUN’S METHOD
tn(i+1)=tn(i)+h;
xn(i+1)=xn(i)+0.5*(fx(xn(i),yn(i),tn(i))+fx(xn(i+1),yn(i+1),tn(i+1)))*h;
yn(i+1)=yn(i)+0.5*(fy(xn(i),yn(i),tn(i))+fy(xn(i+1),yn(i+1),tn(i+1)))*h;
fprintf(‘t=%0.2ft x=%0.3ft y=%0.3fn’,tn(i),xn(i),yn(i))
end
%%%LEAST SQUARE METHOD-FINDS POLYNOMIAL FOR GIVEN DATA SET%%%%%
%INPUT SECTION for Least-Square
X=xn;
Y=yn;
%%__CALCULATIONS SECTION__%%
k=length(X); %NUMBER OF AVAILABLE DATA POINTS
m=n+1; %SIZE OF THE COEFFICENT MATRIX
A=zeros(m,m); %COEFFICENT MATRIX
for j=1:m
for i=1:m
A(j,i)=sum(X.^(i+j-2));
end
end
B=zeros(m,1); %FORCING FUNCTION VECTOR
for i=1:m;
B(i)=sum(Y.*X.^(i-1));
end
a1=AB %COEFFICIENTS FOR THE POLYNOMINAL–> y=a0+a1*x+a2*x^2….an*x^n CAN BE REPLACED BY GAUSSIAN ELIMINATION
%%%%%=========GAUSSIAN ELIMINATION TO FIND "a"========%%%%%%
%%%INPUT SECTION
%CALCULATION SECTION
AB=[A B]; %Augumentent matrix
R=size(AB,1); %# OF ROWS IN AB
C=size(AB,2); %# OF COLUMNS IN AB
%%%%FOWARD ELIMINATION SECTION
for J=1:R-1
[M,I]=max(abs(AB(J:R,J))); %M=MAXIMUM VALUE, I=LOCATION OF THE MAXIMUM VALUE IN THE 1ST ROW
temp=AB(J,:);
AB(J,:)=AB(I+(J-1),:);
AB(I+(J-1),:)=temp;
for i=(J+1):R;
if AB(i,J)~=0;
AB(i,:)=AB(i,:)-(AB(i,J)/AB(J,J))*AB(J,:);
end
end
end
%%%%BACKWARDS SUBSTITUTION
a(R)=AB(R,C)/AB(R,R);
for i=R-1:-1:1
a(i)=(AB(i,C)-AB(i,i+1:R)*a(i+1:R)’)/AB(i,i);
end
disp(a)
syms X
P=0;
for i=1:m;
TT=a(i)*X^(i-1); %T=INDIVIDUAL POLYNOMIAL TERMS
P=P+TT;
end
display(P)
%========END OF GAUSSIAN ELIMINATION=======%%%%%%%% multiplying function handle, polynomial that best fits, heun’s method MATLAB Answers — New Questions
​
Solving a system of ODE over different intervals (with different conditions on parameters)
Good Evening dear Matlab Community,
Now that my first part of code works, I have the ambition to make it a little complicated but I can’t figure out how to implement my plan in matlab:
in the code below you can see that I’m solving a system of two differential equations. Now my challenge is that I would like to solve it with different conditions on certain parameters over the full interval l. I would like to divide the full interval l into sections i each itself divided into two zones, j.
Each section i starts with a conductive zone (j=1) of say l=3 meter for which:
Aap=lcont
Acp=Aap
hap=17
kap=0.018
And ends with a convection zone (j=2) of l=2 meter for which following is true:
Aap=2*lcont
Acp=0
hap=20
kap=0.02
After this, comes the conductive zone of the next section…
I am confused about how to define indices for the sections and zones and whether I need to include loops within the functions corresponding to my ODE in order to solve my system. I have tried (and failed) by introducing following lines within the script of my ODE functions:
for j=1
Aap=lcont
Acp=Aap
hap=17
kap=0.018
end
for j=2
Aap=2*lcont
Acp=0
hap=20
kap=0.02
end
If I define the array J=[1 2], how can I make sure that the same indice is used to solve both ODE for each section?
Ideally I would even like to be able to build-in variation for the parameters of the conductive zone between two different sections (like for section 1 and j=1, hap=17 and for section 2 and j=1, hap=5) but I guess once I understand how to implement it for one case, I’ll be able to extend it to further! 🙂
[l,y]=ode45(@myODE,[0 45],[0.48,30]);
plot(l,y(:,:))
function dy = myODE(l,y)
global u
u=y(1);
global Tp
Tp=y(2);
dTpdl = myODE1(l,u,Tp);
dudl = myODE2(l,u,Tp);
dy = [dudl;dTpdl];
end
function dudl = myODE2(l,u,Tp)
lcont=2.855;
Psatp = 0.13*exp(18-3800/(Tp+227.03));
Phi= 1-exp(-(47*u^2+0.1*Tp*u^1.06));
Ppw= Psatp*Phi;
dudl=-(kap*Aap)*(Ppw/(Tp+273.15)-0.19);
end
function dTpdl=myODE1(l,u,Tp)
lcont=2.855;
hcp0=0.65;
Psatp = 0.13*exp(18-3800/(Tp+227.03));
Phi= 1-exp(-(47*u^2+0.1*Tp*u^1.06));
Ppw= Psatp*Phi;
DHs = 0.1*(u^1.1)*((Tp+275.15)^2)*(1-Phi)/Phi;
DHvap = 1000*(2501-2.3*Tp);
DHevap = DHs+DHvap;
dTpdl=((hcp0+0.955*u)*(140-Tp)*Acp+hap*(100-Tp)*Aap+DHevap*(-kap*Aap)*(Ppw/(Tp+273)-0.19))/(1.4+4.18*u);
endGood Evening dear Matlab Community,
Now that my first part of code works, I have the ambition to make it a little complicated but I can’t figure out how to implement my plan in matlab:
in the code below you can see that I’m solving a system of two differential equations. Now my challenge is that I would like to solve it with different conditions on certain parameters over the full interval l. I would like to divide the full interval l into sections i each itself divided into two zones, j.
Each section i starts with a conductive zone (j=1) of say l=3 meter for which:
Aap=lcont
Acp=Aap
hap=17
kap=0.018
And ends with a convection zone (j=2) of l=2 meter for which following is true:
Aap=2*lcont
Acp=0
hap=20
kap=0.02
After this, comes the conductive zone of the next section…
I am confused about how to define indices for the sections and zones and whether I need to include loops within the functions corresponding to my ODE in order to solve my system. I have tried (and failed) by introducing following lines within the script of my ODE functions:
for j=1
Aap=lcont
Acp=Aap
hap=17
kap=0.018
end
for j=2
Aap=2*lcont
Acp=0
hap=20
kap=0.02
end
If I define the array J=[1 2], how can I make sure that the same indice is used to solve both ODE for each section?
Ideally I would even like to be able to build-in variation for the parameters of the conductive zone between two different sections (like for section 1 and j=1, hap=17 and for section 2 and j=1, hap=5) but I guess once I understand how to implement it for one case, I’ll be able to extend it to further! 🙂
[l,y]=ode45(@myODE,[0 45],[0.48,30]);
plot(l,y(:,:))
function dy = myODE(l,y)
global u
u=y(1);
global Tp
Tp=y(2);
dTpdl = myODE1(l,u,Tp);
dudl = myODE2(l,u,Tp);
dy = [dudl;dTpdl];
end
function dudl = myODE2(l,u,Tp)
lcont=2.855;
Psatp = 0.13*exp(18-3800/(Tp+227.03));
Phi= 1-exp(-(47*u^2+0.1*Tp*u^1.06));
Ppw= Psatp*Phi;
dudl=-(kap*Aap)*(Ppw/(Tp+273.15)-0.19);
end
function dTpdl=myODE1(l,u,Tp)
lcont=2.855;
hcp0=0.65;
Psatp = 0.13*exp(18-3800/(Tp+227.03));
Phi= 1-exp(-(47*u^2+0.1*Tp*u^1.06));
Ppw= Psatp*Phi;
DHs = 0.1*(u^1.1)*((Tp+275.15)^2)*(1-Phi)/Phi;
DHvap = 1000*(2501-2.3*Tp);
DHevap = DHs+DHvap;
dTpdl=((hcp0+0.955*u)*(140-Tp)*Acp+hap*(100-Tp)*Aap+DHevap*(-kap*Aap)*(Ppw/(Tp+273)-0.19))/(1.4+4.18*u);
end Good Evening dear Matlab Community,
Now that my first part of code works, I have the ambition to make it a little complicated but I can’t figure out how to implement my plan in matlab:
in the code below you can see that I’m solving a system of two differential equations. Now my challenge is that I would like to solve it with different conditions on certain parameters over the full interval l. I would like to divide the full interval l into sections i each itself divided into two zones, j.
Each section i starts with a conductive zone (j=1) of say l=3 meter for which:
Aap=lcont
Acp=Aap
hap=17
kap=0.018
And ends with a convection zone (j=2) of l=2 meter for which following is true:
Aap=2*lcont
Acp=0
hap=20
kap=0.02
After this, comes the conductive zone of the next section…
I am confused about how to define indices for the sections and zones and whether I need to include loops within the functions corresponding to my ODE in order to solve my system. I have tried (and failed) by introducing following lines within the script of my ODE functions:
for j=1
Aap=lcont
Acp=Aap
hap=17
kap=0.018
end
for j=2
Aap=2*lcont
Acp=0
hap=20
kap=0.02
end
If I define the array J=[1 2], how can I make sure that the same indice is used to solve both ODE for each section?
Ideally I would even like to be able to build-in variation for the parameters of the conductive zone between two different sections (like for section 1 and j=1, hap=17 and for section 2 and j=1, hap=5) but I guess once I understand how to implement it for one case, I’ll be able to extend it to further! 🙂
[l,y]=ode45(@myODE,[0 45],[0.48,30]);
plot(l,y(:,:))
function dy = myODE(l,y)
global u
u=y(1);
global Tp
Tp=y(2);
dTpdl = myODE1(l,u,Tp);
dudl = myODE2(l,u,Tp);
dy = [dudl;dTpdl];
end
function dudl = myODE2(l,u,Tp)
lcont=2.855;
Psatp = 0.13*exp(18-3800/(Tp+227.03));
Phi= 1-exp(-(47*u^2+0.1*Tp*u^1.06));
Ppw= Psatp*Phi;
dudl=-(kap*Aap)*(Ppw/(Tp+273.15)-0.19);
end
function dTpdl=myODE1(l,u,Tp)
lcont=2.855;
hcp0=0.65;
Psatp = 0.13*exp(18-3800/(Tp+227.03));
Phi= 1-exp(-(47*u^2+0.1*Tp*u^1.06));
Ppw= Psatp*Phi;
DHs = 0.1*(u^1.1)*((Tp+275.15)^2)*(1-Phi)/Phi;
DHvap = 1000*(2501-2.3*Tp);
DHevap = DHs+DHvap;
dTpdl=((hcp0+0.955*u)*(140-Tp)*Acp+hap*(100-Tp)*Aap+DHevap*(-kap*Aap)*(Ppw/(Tp+273)-0.19))/(1.4+4.18*u);
end indice, ode, code, functions, loops MATLAB Answers — New Questions
​
Cannot open file anymore when converting Nifti to DICOM
I have a script where I convert MRI nifti files to DICOM. This used to work but now I get an error when trying to create a new folder and saving the converted files into that folder:
First steps work (Part I):
%% load data
% REVISIT – adapt all text between ‘apostrophes’
patientID = ‘patient180’; % this is the folder with the specific patient data
patientIn = fullfile(‘7Tseg_Brainlab’, patientID); % this is the folder where the patient folders are in
patientFolder = fullfile… % this is the whole path to the patientIn folder
(‘here is whole path’,patientIn);
addpath(genpath(patientIn));
dicom_template = ‘00501_3D_TSE_0.7mm’; % this is the original dicom that is used for the new dicom header
Then with this part I get an error (Part II):
% note: the [space] before the second ‘ after the names is necessary,
% otherwise you will get an error
cmdNiftiToDicom = [‘niftitodicom.exe -o’ patientFolder ‘LmSTN ‘ patientFolder ‘7T2LRmSTN.nii.gz ‘ patientFolder ‘0501_3D_TSE_0.7mm0001.dcm –sagittal ‘ ‘–series-description LmSTN ‘];
statusNiftiToDicom = system( cmdNiftiToDicom );
cmdNiftiToDicom = [‘niftitodicom.exe -o’ patientFolder ‘RmSTN ‘ patientFolder ‘7T2LRmSTN.nii.gz ‘ patientFolder ‘0501_3D_TSE_0.7mm0001.dcm –sagittal ‘ ‘–series-description RmSTN ‘];
statusNiftiToDicom = system( cmdNiftiToDicom);
So what I want is 2 folders, named ‘LmSTN’ and ‘RmSTN’. This used to work and now I get the error saying ‘Cannot open file’, but I do use the correct path.I have a script where I convert MRI nifti files to DICOM. This used to work but now I get an error when trying to create a new folder and saving the converted files into that folder:
First steps work (Part I):
%% load data
% REVISIT – adapt all text between ‘apostrophes’
patientID = ‘patient180’; % this is the folder with the specific patient data
patientIn = fullfile(‘7Tseg_Brainlab’, patientID); % this is the folder where the patient folders are in
patientFolder = fullfile… % this is the whole path to the patientIn folder
(‘here is whole path’,patientIn);
addpath(genpath(patientIn));
dicom_template = ‘00501_3D_TSE_0.7mm’; % this is the original dicom that is used for the new dicom header
Then with this part I get an error (Part II):
% note: the [space] before the second ‘ after the names is necessary,
% otherwise you will get an error
cmdNiftiToDicom = [‘niftitodicom.exe -o’ patientFolder ‘LmSTN ‘ patientFolder ‘7T2LRmSTN.nii.gz ‘ patientFolder ‘0501_3D_TSE_0.7mm0001.dcm –sagittal ‘ ‘–series-description LmSTN ‘];
statusNiftiToDicom = system( cmdNiftiToDicom );
cmdNiftiToDicom = [‘niftitodicom.exe -o’ patientFolder ‘RmSTN ‘ patientFolder ‘7T2LRmSTN.nii.gz ‘ patientFolder ‘0501_3D_TSE_0.7mm0001.dcm –sagittal ‘ ‘–series-description RmSTN ‘];
statusNiftiToDicom = system( cmdNiftiToDicom);
So what I want is 2 folders, named ‘LmSTN’ and ‘RmSTN’. This used to work and now I get the error saying ‘Cannot open file’, but I do use the correct path. I have a script where I convert MRI nifti files to DICOM. This used to work but now I get an error when trying to create a new folder and saving the converted files into that folder:
First steps work (Part I):
%% load data
% REVISIT – adapt all text between ‘apostrophes’
patientID = ‘patient180’; % this is the folder with the specific patient data
patientIn = fullfile(‘7Tseg_Brainlab’, patientID); % this is the folder where the patient folders are in
patientFolder = fullfile… % this is the whole path to the patientIn folder
(‘here is whole path’,patientIn);
addpath(genpath(patientIn));
dicom_template = ‘00501_3D_TSE_0.7mm’; % this is the original dicom that is used for the new dicom header
Then with this part I get an error (Part II):
% note: the [space] before the second ‘ after the names is necessary,
% otherwise you will get an error
cmdNiftiToDicom = [‘niftitodicom.exe -o’ patientFolder ‘LmSTN ‘ patientFolder ‘7T2LRmSTN.nii.gz ‘ patientFolder ‘0501_3D_TSE_0.7mm0001.dcm –sagittal ‘ ‘–series-description LmSTN ‘];
statusNiftiToDicom = system( cmdNiftiToDicom );
cmdNiftiToDicom = [‘niftitodicom.exe -o’ patientFolder ‘RmSTN ‘ patientFolder ‘7T2LRmSTN.nii.gz ‘ patientFolder ‘0501_3D_TSE_0.7mm0001.dcm –sagittal ‘ ‘–series-description RmSTN ‘];
statusNiftiToDicom = system( cmdNiftiToDicom);
So what I want is 2 folders, named ‘LmSTN’ and ‘RmSTN’. This used to work and now I get the error saying ‘Cannot open file’, but I do use the correct path. nifti, dicom MATLAB Answers — New Questions
​
Mouse often freezes for several seconds when MATLAB is open, even when not running
When MATLAB is open, my laptop often freezes for several seconds. This happens frequently, about every half a minute or more often. It happens more often when I am actively using MATLAB but it also happens often when I have MATLAB open but not running or typing anything. I tried different releases (2021b and 2024a) and reset my preferences but that didn’t work. Anyone knows how to solve this?When MATLAB is open, my laptop often freezes for several seconds. This happens frequently, about every half a minute or more often. It happens more often when I am actively using MATLAB but it also happens often when I have MATLAB open but not running or typing anything. I tried different releases (2021b and 2024a) and reset my preferences but that didn’t work. Anyone knows how to solve this? When MATLAB is open, my laptop often freezes for several seconds. This happens frequently, about every half a minute or more often. It happens more often when I am actively using MATLAB but it also happens often when I have MATLAB open but not running or typing anything. I tried different releases (2021b and 2024a) and reset my preferences but that didn’t work. Anyone knows how to solve this? freeze MATLAB Answers — New Questions
​
How can I programmatically link a requirement to a step in test assessment
I want to link my requirements from requirements editor to specific steps in my test assessment block (I also use test scenarios). I was able to create the link between both via mouse-clicks, but I want to link it via a script.
I suppose it is done by the command
slreq.createLink(src,dest)
but I was not able to get the handle / id / url of the step in my test scenario to use as a source (or destination).
If I examine a manually created link it uses the following "adress" to the step in the assessment:
domain: ‘linktype_rmi_simulink’
artifact: ‘C:pathtomodelAutomationExampleUnit.slx’
id: ‘:urn:uuid:4c7edae6-0d2c-4603-96ec-d8f11e1c3afb:52:84’
but where can I find this id ?
thanks in advance !!I want to link my requirements from requirements editor to specific steps in my test assessment block (I also use test scenarios). I was able to create the link between both via mouse-clicks, but I want to link it via a script.
I suppose it is done by the command
slreq.createLink(src,dest)
but I was not able to get the handle / id / url of the step in my test scenario to use as a source (or destination).
If I examine a manually created link it uses the following "adress" to the step in the assessment:
domain: ‘linktype_rmi_simulink’
artifact: ‘C:pathtomodelAutomationExampleUnit.slx’
id: ‘:urn:uuid:4c7edae6-0d2c-4603-96ec-d8f11e1c3afb:52:84’
but where can I find this id ?
thanks in advance !! I want to link my requirements from requirements editor to specific steps in my test assessment block (I also use test scenarios). I was able to create the link between both via mouse-clicks, but I want to link it via a script.
I suppose it is done by the command
slreq.createLink(src,dest)
but I was not able to get the handle / id / url of the step in my test scenario to use as a source (or destination).
If I examine a manually created link it uses the following "adress" to the step in the assessment:
domain: ‘linktype_rmi_simulink’
artifact: ‘C:pathtomodelAutomationExampleUnit.slx’
id: ‘:urn:uuid:4c7edae6-0d2c-4603-96ec-d8f11e1c3afb:52:84’
but where can I find this id ?
thanks in advance !! assessment, step, scenario, requirement, link, programming MATLAB Answers — New Questions
​
Problems by calculating zero points of a cubic function
Hey there,
I have the following problem: let´s say I want to calculate the zero points of the cubic function:
f(x) = -2 x^3 + x^2 + 0.5 x – 8
I already know the respective solution:
p = roots([-2 1 0.5 -8])
The answer p is a vector with all three possible solutions.
Now my problem:
I would like to vary the third coefficient – that is 0.5 – by several values 0.1, 0.2, … , 0.5
That would be like
x=0.1:0.1:0.5;
p = roots([-2 1 x -8])
The problem is that the respective solutions are wrong.
What is my mistake?
How should I do it instead?
Thx a lot in advance!
Best regards,
TimHey there,
I have the following problem: let´s say I want to calculate the zero points of the cubic function:
f(x) = -2 x^3 + x^2 + 0.5 x – 8
I already know the respective solution:
p = roots([-2 1 0.5 -8])
The answer p is a vector with all three possible solutions.
Now my problem:
I would like to vary the third coefficient – that is 0.5 – by several values 0.1, 0.2, … , 0.5
That would be like
x=0.1:0.1:0.5;
p = roots([-2 1 x -8])
The problem is that the respective solutions are wrong.
What is my mistake?
How should I do it instead?
Thx a lot in advance!
Best regards,
Tim Hey there,
I have the following problem: let´s say I want to calculate the zero points of the cubic function:
f(x) = -2 x^3 + x^2 + 0.5 x – 8
I already know the respective solution:
p = roots([-2 1 0.5 -8])
The answer p is a vector with all three possible solutions.
Now my problem:
I would like to vary the third coefficient – that is 0.5 – by several values 0.1, 0.2, … , 0.5
That would be like
x=0.1:0.1:0.5;
p = roots([-2 1 x -8])
The problem is that the respective solutions are wrong.
What is my mistake?
How should I do it instead?
Thx a lot in advance!
Best regards,
Tim roots polynom or cubic funtion MATLAB Answers — New Questions
​
Problem using diary with standalone application
I am developing a MATLAB app using the appdesigner (MATLAB version R2019b), which is intented to be used as standalone ‘.exe’ on Windows. For traceablity I wanted to include a log-file of all outputs. The ‘diary()’ function seemed to be a good function for this. In my code I have a lot of ‘disp(…)’ commands (also in code parts that I cannot or do not want to change). The output for these disp-commands works perfectly in the MATLAB command window when I run the application within the MATLAB environment. However, when run it as Windows standalone the behavior is not logical to me: In the WIndows cmd window the output is the same as in the MATLAB command window, but the diary file only logs the output for the ‘startupFcn()’ and the ‘UIFigureCloseRequest()’ functions and not for any callback, e.g. here from a button ‘DispHelloWorldButtonPushed()’.
Does anybody know how to solve this issue and where it comes from? Btw, also the build-in ‘Create log file’ option during compilation shows the same behavior as the ‘diary’ function.
Here is a minimal working example of an app with this behavior:
classdef Test < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
DispHelloWorldButton matlab.ui.control.Button
end
% Callbacks that handle component events
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
diary(fullfile(pwd,’log.txt’))
diary on
disp(‘Hello World!’)
end
% Button pushed function: DispHelloWorldButton
function DispHelloWorldButtonPushed(app, event)
diary on
disp(‘Button: Hello World!’)
end
% Close request function: UIFigure
function UIFigureCloseRequest(app, event)
disp(‘Bye bye World!’)
diary off
delete(app)
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure and hide until all components are created
app.UIFigure = uifigure(‘Visible’, ‘off’);
app.UIFigure.Position = [100 100 640 480];
app.UIFigure.Name = ‘UI Figure’;
app.UIFigure.CloseRequestFcn = createCallbackFcn(app, @UIFigureCloseRequest, true);
% Create DispHelloWorldButton
app.DispHelloWorldButton = uibutton(app.UIFigure, ‘push’);
app.DispHelloWorldButton.ButtonPushedFcn = createCallbackFcn(app, @DispHelloWorldButtonPushed, true);
app.DispHelloWorldButton.Position = [159 161 311 175];
app.DispHelloWorldButton.Text = ‘Disp(”Hello World!”)’;
% Show the figure after all components are created
app.UIFigure.Visible = ‘on’;
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = Test
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
% Execute the startup function
runStartupFcn(app, @startupFcn)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
endI am developing a MATLAB app using the appdesigner (MATLAB version R2019b), which is intented to be used as standalone ‘.exe’ on Windows. For traceablity I wanted to include a log-file of all outputs. The ‘diary()’ function seemed to be a good function for this. In my code I have a lot of ‘disp(…)’ commands (also in code parts that I cannot or do not want to change). The output for these disp-commands works perfectly in the MATLAB command window when I run the application within the MATLAB environment. However, when run it as Windows standalone the behavior is not logical to me: In the WIndows cmd window the output is the same as in the MATLAB command window, but the diary file only logs the output for the ‘startupFcn()’ and the ‘UIFigureCloseRequest()’ functions and not for any callback, e.g. here from a button ‘DispHelloWorldButtonPushed()’.
Does anybody know how to solve this issue and where it comes from? Btw, also the build-in ‘Create log file’ option during compilation shows the same behavior as the ‘diary’ function.
Here is a minimal working example of an app with this behavior:
classdef Test < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
DispHelloWorldButton matlab.ui.control.Button
end
% Callbacks that handle component events
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
diary(fullfile(pwd,’log.txt’))
diary on
disp(‘Hello World!’)
end
% Button pushed function: DispHelloWorldButton
function DispHelloWorldButtonPushed(app, event)
diary on
disp(‘Button: Hello World!’)
end
% Close request function: UIFigure
function UIFigureCloseRequest(app, event)
disp(‘Bye bye World!’)
diary off
delete(app)
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure and hide until all components are created
app.UIFigure = uifigure(‘Visible’, ‘off’);
app.UIFigure.Position = [100 100 640 480];
app.UIFigure.Name = ‘UI Figure’;
app.UIFigure.CloseRequestFcn = createCallbackFcn(app, @UIFigureCloseRequest, true);
% Create DispHelloWorldButton
app.DispHelloWorldButton = uibutton(app.UIFigure, ‘push’);
app.DispHelloWorldButton.ButtonPushedFcn = createCallbackFcn(app, @DispHelloWorldButtonPushed, true);
app.DispHelloWorldButton.Position = [159 161 311 175];
app.DispHelloWorldButton.Text = ‘Disp(”Hello World!”)’;
% Show the figure after all components are created
app.UIFigure.Visible = ‘on’;
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = Test
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
% Execute the startup function
runStartupFcn(app, @startupFcn)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
end I am developing a MATLAB app using the appdesigner (MATLAB version R2019b), which is intented to be used as standalone ‘.exe’ on Windows. For traceablity I wanted to include a log-file of all outputs. The ‘diary()’ function seemed to be a good function for this. In my code I have a lot of ‘disp(…)’ commands (also in code parts that I cannot or do not want to change). The output for these disp-commands works perfectly in the MATLAB command window when I run the application within the MATLAB environment. However, when run it as Windows standalone the behavior is not logical to me: In the WIndows cmd window the output is the same as in the MATLAB command window, but the diary file only logs the output for the ‘startupFcn()’ and the ‘UIFigureCloseRequest()’ functions and not for any callback, e.g. here from a button ‘DispHelloWorldButtonPushed()’.
Does anybody know how to solve this issue and where it comes from? Btw, also the build-in ‘Create log file’ option during compilation shows the same behavior as the ‘diary’ function.
Here is a minimal working example of an app with this behavior:
classdef Test < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
DispHelloWorldButton matlab.ui.control.Button
end
% Callbacks that handle component events
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
diary(fullfile(pwd,’log.txt’))
diary on
disp(‘Hello World!’)
end
% Button pushed function: DispHelloWorldButton
function DispHelloWorldButtonPushed(app, event)
diary on
disp(‘Button: Hello World!’)
end
% Close request function: UIFigure
function UIFigureCloseRequest(app, event)
disp(‘Bye bye World!’)
diary off
delete(app)
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure and hide until all components are created
app.UIFigure = uifigure(‘Visible’, ‘off’);
app.UIFigure.Position = [100 100 640 480];
app.UIFigure.Name = ‘UI Figure’;
app.UIFigure.CloseRequestFcn = createCallbackFcn(app, @UIFigureCloseRequest, true);
% Create DispHelloWorldButton
app.DispHelloWorldButton = uibutton(app.UIFigure, ‘push’);
app.DispHelloWorldButton.ButtonPushedFcn = createCallbackFcn(app, @DispHelloWorldButtonPushed, true);
app.DispHelloWorldButton.Position = [159 161 311 175];
app.DispHelloWorldButton.Text = ‘Disp(”Hello World!”)’;
% Show the figure after all components are created
app.UIFigure.Visible = ‘on’;
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = Test
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
% Execute the startup function
runStartupFcn(app, @startupFcn)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
end standalone, disp, diary, callback, appdesigner MATLAB Answers — New Questions
​
How can I export .mat file to .xls file?
How can I export .mat file to .xls file? I am using xlswrite(‘filename.xls’) but not working. I have 50×50 deta. Kindly help me.How can I export .mat file to .xls file? I am using xlswrite(‘filename.xls’) but not working. I have 50×50 deta. Kindly help me. How can I export .mat file to .xls file? I am using xlswrite(‘filename.xls’) but not working. I have 50×50 deta. Kindly help me. how can i export .mat file to .xls file? MATLAB Answers — New Questions
​