Category: Matlab
Category Archives: Matlab
I have an equation. I want to adjust one of the values in the equation until it hits a certain output. How do I run a loop to increment that certain value until it does this?
equation: X/Y=P
Increase Y by an increment of 1 until gives an output equal or less than P.
Example:
X=21
Y=4
P=3
equation: 21/4=5.25
GOAL: increment Y unitl it gives an output less than or equal to P.equation: X/Y=P
Increase Y by an increment of 1 until gives an output equal or less than P.
Example:
X=21
Y=4
P=3
equation: 21/4=5.25
GOAL: increment Y unitl it gives an output less than or equal to P. equation: X/Y=P
Increase Y by an increment of 1 until gives an output equal or less than P.
Example:
X=21
Y=4
P=3
equation: 21/4=5.25
GOAL: increment Y unitl it gives an output less than or equal to P. if loop, for loop, if else, if else statement MATLAB Answers — New Questions
transformPointsForward does not support PolynomialTransformation2D
I would like to know why transformPointsForward does not support PolynomialTransformation2D as a transformation object. It is weird because transformPointsInverse does support PolynomialTransformation2D.
So the workaround is simple, instead of using:
tform = fitgeotform2d(movingPoints,fixedPoints,"polynomial",degree)
[x,y] = transformPointsForward(tform,u,v)
I used:
tform = fitgeotform2d(fixedPoints,movingPoints,"polynomial",degree)
[x,y] = transformPointsInverse(tform,u,v)
But it is still counterintuitive and increases the risk of making mistakes later in the code.
Please let me know if there is something I am overlooking or if this is really a missing functionality of the transformPointsForward function.
This is the error I am getting when using transformPointsForward:
Incorrect number or types of inputs or outputs for function transformPointsForward.I would like to know why transformPointsForward does not support PolynomialTransformation2D as a transformation object. It is weird because transformPointsInverse does support PolynomialTransformation2D.
So the workaround is simple, instead of using:
tform = fitgeotform2d(movingPoints,fixedPoints,"polynomial",degree)
[x,y] = transformPointsForward(tform,u,v)
I used:
tform = fitgeotform2d(fixedPoints,movingPoints,"polynomial",degree)
[x,y] = transformPointsInverse(tform,u,v)
But it is still counterintuitive and increases the risk of making mistakes later in the code.
Please let me know if there is something I am overlooking or if this is really a missing functionality of the transformPointsForward function.
This is the error I am getting when using transformPointsForward:
Incorrect number or types of inputs or outputs for function transformPointsForward. I would like to know why transformPointsForward does not support PolynomialTransformation2D as a transformation object. It is weird because transformPointsInverse does support PolynomialTransformation2D.
So the workaround is simple, instead of using:
tform = fitgeotform2d(movingPoints,fixedPoints,"polynomial",degree)
[x,y] = transformPointsForward(tform,u,v)
I used:
tform = fitgeotform2d(fixedPoints,movingPoints,"polynomial",degree)
[x,y] = transformPointsInverse(tform,u,v)
But it is still counterintuitive and increases the risk of making mistakes later in the code.
Please let me know if there is something I am overlooking or if this is really a missing functionality of the transformPointsForward function.
This is the error I am getting when using transformPointsForward:
Incorrect number or types of inputs or outputs for function transformPointsForward. image processing MATLAB Answers — New Questions
vpa, simplify, solve, etc don’t fully simplify symbolic function.
I am doing integration using MATLAB’s symbolic toolbox. I have many functions of my independent variable, z. This is my code, with numbers changed and the problem simplified. I have F1 print at the end just so I can see what the function resolves to.
l = 50;
a = 10;
W = 1200;
syms Q(z);
Q(z) = 1200 + 56*z – 2.5*z^2;
R = – int(Q(z), [0 l]) + W;
syms F1(z) F2(z) F3(z);
F1(z) = simplify(- int(Q(z), [0 z]) – R,"IgnoreAnalyticConstraints",true);
F1
Even with simplify and "IgnoreAnalyticConstraints" being set to true, F1 is in the form "73900/3 – (z*(- 5*z^2 + 168*z + 7200))/6" instead of az^3/b + cz^2/d + ez/f + g, the fully simplified method. vpa just puts all fractions in numeric form, and eval doesn’t really do anything. What am I missing, as I know that there should be some method to this simple operation of simplifying a polynomial.I am doing integration using MATLAB’s symbolic toolbox. I have many functions of my independent variable, z. This is my code, with numbers changed and the problem simplified. I have F1 print at the end just so I can see what the function resolves to.
l = 50;
a = 10;
W = 1200;
syms Q(z);
Q(z) = 1200 + 56*z – 2.5*z^2;
R = – int(Q(z), [0 l]) + W;
syms F1(z) F2(z) F3(z);
F1(z) = simplify(- int(Q(z), [0 z]) – R,"IgnoreAnalyticConstraints",true);
F1
Even with simplify and "IgnoreAnalyticConstraints" being set to true, F1 is in the form "73900/3 – (z*(- 5*z^2 + 168*z + 7200))/6" instead of az^3/b + cz^2/d + ez/f + g, the fully simplified method. vpa just puts all fractions in numeric form, and eval doesn’t really do anything. What am I missing, as I know that there should be some method to this simple operation of simplifying a polynomial. I am doing integration using MATLAB’s symbolic toolbox. I have many functions of my independent variable, z. This is my code, with numbers changed and the problem simplified. I have F1 print at the end just so I can see what the function resolves to.
l = 50;
a = 10;
W = 1200;
syms Q(z);
Q(z) = 1200 + 56*z – 2.5*z^2;
R = – int(Q(z), [0 l]) + W;
syms F1(z) F2(z) F3(z);
F1(z) = simplify(- int(Q(z), [0 z]) – R,"IgnoreAnalyticConstraints",true);
F1
Even with simplify and "IgnoreAnalyticConstraints" being set to true, F1 is in the form "73900/3 – (z*(- 5*z^2 + 168*z + 7200))/6" instead of az^3/b + cz^2/d + ez/f + g, the fully simplified method. vpa just puts all fractions in numeric form, and eval doesn’t really do anything. What am I missing, as I know that there should be some method to this simple operation of simplifying a polynomial. symbolic, function MATLAB Answers — New Questions
Why do I receive a mask initialization error when I run Simulink file in a newer version of Simulink?
Hi,
I can run the Simscape model below in Simulink 2015b:
https://www.mathworks.com/matlabcentral/fileexchange/54771-10-machine-new-england-power-system-ieee-benchmark
However, I am trying to generate a compiled version of this code, which requires using a newer version of Simulink. When I try to run the same code in Simulink 2020b, I get the following error:
"Failed to evaluate mask initialization commands.Caused by:Index exceeds the number of array elements (1)."
It happens in generator G1. I can’t debug it (if I copy and paste the code in Matlab, it can’t find one of the functions").
Any ideas on how to fix this error?Hi,
I can run the Simscape model below in Simulink 2015b:
https://www.mathworks.com/matlabcentral/fileexchange/54771-10-machine-new-england-power-system-ieee-benchmark
However, I am trying to generate a compiled version of this code, which requires using a newer version of Simulink. When I try to run the same code in Simulink 2020b, I get the following error:
"Failed to evaluate mask initialization commands.Caused by:Index exceeds the number of array elements (1)."
It happens in generator G1. I can’t debug it (if I copy and paste the code in Matlab, it can’t find one of the functions").
Any ideas on how to fix this error? Hi,
I can run the Simscape model below in Simulink 2015b:
https://www.mathworks.com/matlabcentral/fileexchange/54771-10-machine-new-england-power-system-ieee-benchmark
However, I am trying to generate a compiled version of this code, which requires using a newer version of Simulink. When I try to run the same code in Simulink 2020b, I get the following error:
"Failed to evaluate mask initialization commands.Caused by:Index exceeds the number of array elements (1)."
It happens in generator G1. I can’t debug it (if I copy and paste the code in Matlab, it can’t find one of the functions").
Any ideas on how to fix this error? simscape, mask initialization error, power system simulation MATLAB Answers — New Questions
Error de Add-On Installer
Hola, Buenos días.
Estoy intentando instalar las Toolbox Simscape, y Simscape Electrical. Todo va bien hasta que al momento de descargar e instalar las Toolbox aparece el siguiente error.
Estoy usando MATLAB R2024aHola, Buenos días.
Estoy intentando instalar las Toolbox Simscape, y Simscape Electrical. Todo va bien hasta que al momento de descargar e instalar las Toolbox aparece el siguiente error.
Estoy usando MATLAB R2024a Hola, Buenos días.
Estoy intentando instalar las Toolbox Simscape, y Simscape Electrical. Todo va bien hasta que al momento de descargar e instalar las Toolbox aparece el siguiente error.
Estoy usando MATLAB R2024a error, addon, toolbox MATLAB Answers — New Questions
not able to copy paste custom simscape component
I am copying custom component to a default matlab example, But getting below error
–>Error evaluating ‘PreCopyFcn’ callback of Converter
(Three-Phase) block (mask) ‘untitled/Converter’.
Callback string is ‘simscape.compiler.sli.internal.callback(‘PreCopyFcn’,gcbh);’
–>Cannot add block Converter to the model because a license for Simscape Electrical is not available.I am copying custom component to a default matlab example, But getting below error
–>Error evaluating ‘PreCopyFcn’ callback of Converter
(Three-Phase) block (mask) ‘untitled/Converter’.
Callback string is ‘simscape.compiler.sli.internal.callback(‘PreCopyFcn’,gcbh);’
–>Cannot add block Converter to the model because a license for Simscape Electrical is not available. I am copying custom component to a default matlab example, But getting below error
–>Error evaluating ‘PreCopyFcn’ callback of Converter
(Three-Phase) block (mask) ‘untitled/Converter’.
Callback string is ‘simscape.compiler.sli.internal.callback(‘PreCopyFcn’,gcbh);’
–>Cannot add block Converter to the model because a license for Simscape Electrical is not available. #error #simulink MATLAB Answers — New Questions
How to use “wget” command in Linux?
I’m trying to run a program that needs to access the "wget" the command as follows:
[result, cmdout] = system(‘wget -h’);
Do you have any suggestions to get it working in Linux?I’m trying to run a program that needs to access the "wget" the command as follows:
[result, cmdout] = system(‘wget -h’);
Do you have any suggestions to get it working in Linux? I’m trying to run a program that needs to access the "wget" the command as follows:
[result, cmdout] = system(‘wget -h’);
Do you have any suggestions to get it working in Linux? wget, websave, url, download MATLAB Answers — New Questions
Why trapz is giving me negative area?
Hey all,
I have two vectors, frequency and power. I’m trying to get the area under the curve of the power plot but it ends up giving me a negative value. When I do trapz(power) alone, it’s a positive value. diff(freq) is also positive. I’m attaching the data for your reference. Thanks!Hey all,
I have two vectors, frequency and power. I’m trying to get the area under the curve of the power plot but it ends up giving me a negative value. When I do trapz(power) alone, it’s a positive value. diff(freq) is also positive. I’m attaching the data for your reference. Thanks! Hey all,
I have two vectors, frequency and power. I’m trying to get the area under the curve of the power plot but it ends up giving me a negative value. When I do trapz(power) alone, it’s a positive value. diff(freq) is also positive. I’m attaching the data for your reference. Thanks! integral, trapz MATLAB Answers — New Questions
Simulink Code Generation Error: Array of Classes Initalization
Hello,
I am trying to built a Simulink function which keeps record of the statistics of the system by using array of classes. However, I can not generate an array of class in Simulink. I realize that I should instantiate an array in Simulink but how can I instantiate a class?
arrivedArray(3, 65536) = queueInfo;
analysisArray(3, 65536) = queueInfo;
In the picture above, queueInfo is my class. This two lines return with the following error.
Code generation requires variable arrivedArray to be fully defined before subscribing it.
By the way, this is the suggested method by Matlab for creating an array of class but somehow it doesn’t work. Later I’ve tried this one:
persistent arrivedArray
if isempty(arrivedArray)
arrivedArray(3, 655336) = queueInfo;
end
persistent analysisArray
if isempty(analysisArray)
analysisArray(3, 65536) = queueInfo;
end
But it doesn’t work either. The error İs:
Persistent variable ‘arrivedArray’ must be assigned before it is used. The only exception is a check using ‘isempty(arrivedArray)’ that can be performed prior to assignment.
I understand the error but have no idea how to fix it. A constructor did not help me too. Here is my class structure:
1
classdef transactionInfo
properties
tag = uint16(0);
arrivalTime = uint64(0);
departureTime = uint64(0);
end
methods
function obj = transactionInfo(v)
if nargin > 0
obj.tag = uint16(v);
obj.arrivalTime = uint64(v);
obj.departureTime = uint64(v);
end
end
end
end
2
classdef queueInfo < transactionInfo
properties
length = uint16(0);
queueID = uint8(0);
delay = uint64(0);
end
methods
function obj = queueInfo(v)
if nargin > 0
obj.length = uint16(v);
obj.queueID = uint8(v);
obj.delay = uint64(v);
end
end
end
end
Does anyone know how to fix this issue?
Note: There might be some obvious errors in my class structures, I am kind of new to OOP. Every suggestion is welcomed.Hello,
I am trying to built a Simulink function which keeps record of the statistics of the system by using array of classes. However, I can not generate an array of class in Simulink. I realize that I should instantiate an array in Simulink but how can I instantiate a class?
arrivedArray(3, 65536) = queueInfo;
analysisArray(3, 65536) = queueInfo;
In the picture above, queueInfo is my class. This two lines return with the following error.
Code generation requires variable arrivedArray to be fully defined before subscribing it.
By the way, this is the suggested method by Matlab for creating an array of class but somehow it doesn’t work. Later I’ve tried this one:
persistent arrivedArray
if isempty(arrivedArray)
arrivedArray(3, 655336) = queueInfo;
end
persistent analysisArray
if isempty(analysisArray)
analysisArray(3, 65536) = queueInfo;
end
But it doesn’t work either. The error İs:
Persistent variable ‘arrivedArray’ must be assigned before it is used. The only exception is a check using ‘isempty(arrivedArray)’ that can be performed prior to assignment.
I understand the error but have no idea how to fix it. A constructor did not help me too. Here is my class structure:
1
classdef transactionInfo
properties
tag = uint16(0);
arrivalTime = uint64(0);
departureTime = uint64(0);
end
methods
function obj = transactionInfo(v)
if nargin > 0
obj.tag = uint16(v);
obj.arrivalTime = uint64(v);
obj.departureTime = uint64(v);
end
end
end
end
2
classdef queueInfo < transactionInfo
properties
length = uint16(0);
queueID = uint8(0);
delay = uint64(0);
end
methods
function obj = queueInfo(v)
if nargin > 0
obj.length = uint16(v);
obj.queueID = uint8(v);
obj.delay = uint64(v);
end
end
end
end
Does anyone know how to fix this issue?
Note: There might be some obvious errors in my class structures, I am kind of new to OOP. Every suggestion is welcomed. Hello,
I am trying to built a Simulink function which keeps record of the statistics of the system by using array of classes. However, I can not generate an array of class in Simulink. I realize that I should instantiate an array in Simulink but how can I instantiate a class?
arrivedArray(3, 65536) = queueInfo;
analysisArray(3, 65536) = queueInfo;
In the picture above, queueInfo is my class. This two lines return with the following error.
Code generation requires variable arrivedArray to be fully defined before subscribing it.
By the way, this is the suggested method by Matlab for creating an array of class but somehow it doesn’t work. Later I’ve tried this one:
persistent arrivedArray
if isempty(arrivedArray)
arrivedArray(3, 655336) = queueInfo;
end
persistent analysisArray
if isempty(analysisArray)
analysisArray(3, 65536) = queueInfo;
end
But it doesn’t work either. The error İs:
Persistent variable ‘arrivedArray’ must be assigned before it is used. The only exception is a check using ‘isempty(arrivedArray)’ that can be performed prior to assignment.
I understand the error but have no idea how to fix it. A constructor did not help me too. Here is my class structure:
1
classdef transactionInfo
properties
tag = uint16(0);
arrivalTime = uint64(0);
departureTime = uint64(0);
end
methods
function obj = transactionInfo(v)
if nargin > 0
obj.tag = uint16(v);
obj.arrivalTime = uint64(v);
obj.departureTime = uint64(v);
end
end
end
end
2
classdef queueInfo < transactionInfo
properties
length = uint16(0);
queueID = uint8(0);
delay = uint64(0);
end
methods
function obj = queueInfo(v)
if nargin > 0
obj.length = uint16(v);
obj.queueID = uint8(v);
obj.delay = uint64(v);
end
end
end
end
Does anyone know how to fix this issue?
Note: There might be some obvious errors in my class structures, I am kind of new to OOP. Every suggestion is welcomed. simulink, class, code generation MATLAB Answers — New Questions
How can I count the number of non virtual blocks in my model?
I have a home licenseI have a home license I have a home license non virtual MATLAB Answers — New Questions
How can I write a sparse matrix’s non-zero values and the corresponding row and column information to a text file?
I define a sparse identity matrix, using the SPEYE command as follows:
A = speye(5)
and receive the following output:
A =
(1,1) 1
(2,2) 1
(3,3) 1
(4,4) 1
(5,5) 1
I would like to write the non-zero elements as well as the row and column information corresponding to the non-zero elements to a text file.I define a sparse identity matrix, using the SPEYE command as follows:
A = speye(5)
and receive the following output:
A =
(1,1) 1
(2,2) 1
(3,3) 1
(4,4) 1
(5,5) 1
I would like to write the non-zero elements as well as the row and column information corresponding to the non-zero elements to a text file. I define a sparse identity matrix, using the SPEYE command as follows:
A = speye(5)
and receive the following output:
A =
(1,1) 1
(2,2) 1
(3,3) 1
(4,4) 1
(5,5) 1
I would like to write the non-zero elements as well as the row and column information corresponding to the non-zero elements to a text file. MATLAB Answers — New Questions
Can I use figures generated by view(net) command freely in my thesis and academic papers?
Hello,
I would like to know if I can freely use the figures generated by the view(net) command in my thesis (also in academic papers).
If not, what is the correct way to use them appropriately?
Or
Thank you.Hello,
I would like to know if I can freely use the figures generated by the view(net) command in my thesis (also in academic papers).
If not, what is the correct way to use them appropriately?
Or
Thank you. Hello,
I would like to know if I can freely use the figures generated by the view(net) command in my thesis (also in academic papers).
If not, what is the correct way to use them appropriately?
Or
Thank you. copyright MATLAB Answers — New Questions
The output of ‘chart XYZ’ cannot be logged because this block is a Simulink function within Stateflow or a MATLAB Function block. But no signal is logged.
I’m using Matlab/Simulink 2018b and my model is not runnable. The following error appears: The output of ‘path of chart XYZ’ cannot be logged because this block is a Simulink function within Stateflow or a MATLAB Function block. Even though no signal is logged. I don’t know how to localize the issue. Befor puschen the model to the lib it was working fine. How can I fix the issue?I’m using Matlab/Simulink 2018b and my model is not runnable. The following error appears: The output of ‘path of chart XYZ’ cannot be logged because this block is a Simulink function within Stateflow or a MATLAB Function block. Even though no signal is logged. I don’t know how to localize the issue. Befor puschen the model to the lib it was working fine. How can I fix the issue? I’m using Matlab/Simulink 2018b and my model is not runnable. The following error appears: The output of ‘path of chart XYZ’ cannot be logged because this block is a Simulink function within Stateflow or a MATLAB Function block. Even though no signal is logged. I don’t know how to localize the issue. Befor puschen the model to the lib it was working fine. How can I fix the issue? output cannot be logged MATLAB Answers — New Questions
View into a Simscapeblock
Hi all,
I have a question about Simscape blocks. In particular, I am interested in the block “Spark Ignition Engine”.
It performs various calculations related to the operation of a four-stroke engine with external ignition.
I would like to know what formulas are behind the block’s calculations so that I can better interpret the results.
The “Description” submenu allows you to access the block’s source code.
However, this code only shows what you see in Simulink’s graphical interface.
Is there a way to access the mathematical formulas that the block uses to perform its calculations?
The following command takes you to the example project in question:
openExample(‘sdl/SingleCylinderSparkIgnitionEngineExample’)
I would be very happy to receive a reply.
Kind regards,
FlorianHi all,
I have a question about Simscape blocks. In particular, I am interested in the block “Spark Ignition Engine”.
It performs various calculations related to the operation of a four-stroke engine with external ignition.
I would like to know what formulas are behind the block’s calculations so that I can better interpret the results.
The “Description” submenu allows you to access the block’s source code.
However, this code only shows what you see in Simulink’s graphical interface.
Is there a way to access the mathematical formulas that the block uses to perform its calculations?
The following command takes you to the example project in question:
openExample(‘sdl/SingleCylinderSparkIgnitionEngineExample’)
I would be very happy to receive a reply.
Kind regards,
Florian Hi all,
I have a question about Simscape blocks. In particular, I am interested in the block “Spark Ignition Engine”.
It performs various calculations related to the operation of a four-stroke engine with external ignition.
I would like to know what formulas are behind the block’s calculations so that I can better interpret the results.
The “Description” submenu allows you to access the block’s source code.
However, this code only shows what you see in Simulink’s graphical interface.
Is there a way to access the mathematical formulas that the block uses to perform its calculations?
The following command takes you to the example project in question:
openExample(‘sdl/SingleCylinderSparkIgnitionEngineExample’)
I would be very happy to receive a reply.
Kind regards,
Florian simscape, spark ignition engine, singlecylindersparkignitionengineexample MATLAB Answers — New Questions
Horizontal Freestream Flow Issues With Symbolic Math Toolbox
I am attempting to plot stictly horizontal freestream flow using fcontour and the Symbolic Math toolbox. A few days ago I was able to successfully achieve this with the code below. However, starting last night whenever I run the script the "symbolic method" produces vertical contour lines that indicate stricty vertical freestram flow. I have gone back to the original code to ensure nothing I changed in the script had an impact, but the issue still remains. I have included an image of the incorrect plot (from the symbolic method) vs. the correct plot (from the numerical method) to better illustrate the problem. I would love some guidance on what to try and fix this strange issue.
clear
clc
% use the symbolic method to create the stream function for freestream flow
% and plot it
syms xs ys
Uval = 1;
Vval = 0;
psi_fs = Uval*ys – Vval*xs;
figure(4)
fcontour(psi_fs,[-3 3 -5 5],’k’); %’k’ makes all the lines black
% use the numerical method to create the stream function for freestream
% flow and plot it
xlocs = [-3:.1:3]; % you can use linspace for this
ylocs = [-3:.1:3];
% make the grid of points x horizontal, y vertical
[xmat,ymat] = meshgrid(xlocs,ylocs);
psi_vals_fs = Uval*ymat – Vval*xmat;
figure(5)
contour(xmat, ymat, psi_vals_fs,’k’) % ‘k’ makes all the lines blackI am attempting to plot stictly horizontal freestream flow using fcontour and the Symbolic Math toolbox. A few days ago I was able to successfully achieve this with the code below. However, starting last night whenever I run the script the "symbolic method" produces vertical contour lines that indicate stricty vertical freestram flow. I have gone back to the original code to ensure nothing I changed in the script had an impact, but the issue still remains. I have included an image of the incorrect plot (from the symbolic method) vs. the correct plot (from the numerical method) to better illustrate the problem. I would love some guidance on what to try and fix this strange issue.
clear
clc
% use the symbolic method to create the stream function for freestream flow
% and plot it
syms xs ys
Uval = 1;
Vval = 0;
psi_fs = Uval*ys – Vval*xs;
figure(4)
fcontour(psi_fs,[-3 3 -5 5],’k’); %’k’ makes all the lines black
% use the numerical method to create the stream function for freestream
% flow and plot it
xlocs = [-3:.1:3]; % you can use linspace for this
ylocs = [-3:.1:3];
% make the grid of points x horizontal, y vertical
[xmat,ymat] = meshgrid(xlocs,ylocs);
psi_vals_fs = Uval*ymat – Vval*xmat;
figure(5)
contour(xmat, ymat, psi_vals_fs,’k’) % ‘k’ makes all the lines black I am attempting to plot stictly horizontal freestream flow using fcontour and the Symbolic Math toolbox. A few days ago I was able to successfully achieve this with the code below. However, starting last night whenever I run the script the "symbolic method" produces vertical contour lines that indicate stricty vertical freestram flow. I have gone back to the original code to ensure nothing I changed in the script had an impact, but the issue still remains. I have included an image of the incorrect plot (from the symbolic method) vs. the correct plot (from the numerical method) to better illustrate the problem. I would love some guidance on what to try and fix this strange issue.
clear
clc
% use the symbolic method to create the stream function for freestream flow
% and plot it
syms xs ys
Uval = 1;
Vval = 0;
psi_fs = Uval*ys – Vval*xs;
figure(4)
fcontour(psi_fs,[-3 3 -5 5],’k’); %’k’ makes all the lines black
% use the numerical method to create the stream function for freestream
% flow and plot it
xlocs = [-3:.1:3]; % you can use linspace for this
ylocs = [-3:.1:3];
% make the grid of points x horizontal, y vertical
[xmat,ymat] = meshgrid(xlocs,ylocs);
psi_vals_fs = Uval*ymat – Vval*xmat;
figure(5)
contour(xmat, ymat, psi_vals_fs,’k’) % ‘k’ makes all the lines black fcontour, symbolic MATLAB Answers — New Questions
Why does the MathWorks Product Installer get stuck on selecting the destination folder?
Why does the MathWorks Product Installer get stuck on selecting the destination folder?Why does the MathWorks Product Installer get stuck on selecting the destination folder? Why does the MathWorks Product Installer get stuck on selecting the destination folder? MATLAB Answers — New Questions
Non linear fitting and parametric optimization with genetic algorithm
I have a constitutive model with 4 parameters to fit to experimental stress vs strain data. Before implementing the GA, I tried lsqnonlin (with and without multistart), but one of the main problems is that the algorithm often tends to assign bound values to the optimised parameters. To the first parameter, which is larger than the others, it assigns the upper bound, to the second and third it assigns the lower bounds. Plotting the best individuals over time, it appears that the ga spans little between the solutions, and the optimisation seems to be biased towards the first parameter.
I am using the uniaxial model proposed by Dong, and have rechecked the correctness of the model form several times. Below is the main containing the ga and my error function. Any help is welcome, thank you
%CHANGE LINE 3 TO SELECT THE PATCH
%CHANGE LINES 9 AND 10 TO SELECT THE SPECIMEN
clc; clear all;close all;
load ("data_11122.mat"); % change lot number if necessary
clearvars -except data
%Model requires lambda= x/x0 instead of epsilon: calculate lambda
% from exp data and store in data struct
stress=data(6).stress; %change index to change dogbone if necessary
stress=stress/max(stress);
strain=data(6).strain; %change index to change dogbone if necessary
p_initial=[50 25 5 .5]*10e-3;
a0=[1 0 0];
%vincoli su zeta imposti da dong ( zeta tra 0 e 1 )
lb=[0 0 0 0]; %% AGGIUSTA UNITA DIN MISURA
ub=[650 98 35 1];
%ottimizzazione non lineare con metodo least square ( like Dong )
options = optimoptions(‘ga’, ‘Display’, ‘iter’, ‘PopulationSize’, 100, ‘MaxGenerations’, 200, ‘UseParallel’, true,’NonlinearConstraintAlgorithm’,’penalty’,’PlotFcn’,’gaplotbestindiv’,’InitialPopulationMatrix’,p_initial);
nvars=4;
[p_opt, fval, exitflag, output] = ga(@(p)(error_function(p, strain, stress)), nvars, [], [], [], [], lb, ub, [], options);
%check plot e ricostruzione del modello con i parametri ottimizzati
c = p_opt(1); % c
k1 = p_opt(2); % k1
k2 = p_opt(3); % k2
zeta = p_opt(4); % zeta
for i=1:length(strain)
sigma_calculated(i)=c*(strain(i)^2) + 2*strain(i)^2*k1*((strain(i)^2-1)*(i-zeta)^2*exp(k2*(strain(i)^2-1)^2*(1-zeta)^2));
end
% for i=1:length(strain)
% f = [strain(i) 0 0; %deformation gradient for each value of strain
% 0 1/sqrt(strain(i)) 0;
% 0 0 1/sqrt(strain(i))];
% C = f .* f’; % Calcola il tensore di Cauchy-Green
% prodot = a0 .* a0′;
% I4 = sum(sum(C .* prodot)); % calcolo invariante di C
% I = eye(3); % Matrice identità
%
% sigma= f*c*I*f’ + 2*f*(k1 * (1 – zeta)^2 * (I4 – 1) * (exp(k2 * ((1 – zeta) * (I4 – 1))^2)) * prodot)*f’;
%
% sigma_calculated(i)= sigma(1,1); %componente xx
% end
figure()
plot (strain,stress);
hold on
plot(strain, sigma_calculated,’r’);
legend (‘experimental’, ‘model’,’Location’, ‘northwest’);
function error = error_function(p,strain,stress)
sigma_calculated = zeros(length(strain), 1);
for i = 2:length(strain) %avoiding first value=0;
% f = [strain(i) 0 0; %deformation gradient
% 0 1/sqrt(strain(i)) 0;
% 0 0 1/sqrt(strain(i))];
% C = f .* f’; % tensore di Cauchy-Green
% prodot = a0 .* a0′;
% I4 = a0′ .* C .* a0; % calcolo IV invariante di C
% I = eye(3);
%
% sigma= f*p(1)*I*f’ + 2*f*(p(2) * (1 – p(4))^2 * (I4 – 1) * (exp(p(3) * ((1 – p(4)) * (I4 – 1))^2)) * prodot)*f’;
%
%
% sigma_calculated(i)= sigma(1,1); %componente xx
sigma_calculated(i)=p(1)*(strain(i)^2) + 2*strain(i)^2*p(2)*((strain(i)^2-1)*(i-p(4))^2*exp(p(3)*(strain(i)^2-1)^2*(1-p(4))^2));
end
error= sum((sigma_calculated-stress).^2) ; %/sum(stress).^2; % Somma dei quadrati delle differenze
%aggiungere normalizzazione rispetto allo stress( guardare script marta calcolo error )
endI have a constitutive model with 4 parameters to fit to experimental stress vs strain data. Before implementing the GA, I tried lsqnonlin (with and without multistart), but one of the main problems is that the algorithm often tends to assign bound values to the optimised parameters. To the first parameter, which is larger than the others, it assigns the upper bound, to the second and third it assigns the lower bounds. Plotting the best individuals over time, it appears that the ga spans little between the solutions, and the optimisation seems to be biased towards the first parameter.
I am using the uniaxial model proposed by Dong, and have rechecked the correctness of the model form several times. Below is the main containing the ga and my error function. Any help is welcome, thank you
%CHANGE LINE 3 TO SELECT THE PATCH
%CHANGE LINES 9 AND 10 TO SELECT THE SPECIMEN
clc; clear all;close all;
load ("data_11122.mat"); % change lot number if necessary
clearvars -except data
%Model requires lambda= x/x0 instead of epsilon: calculate lambda
% from exp data and store in data struct
stress=data(6).stress; %change index to change dogbone if necessary
stress=stress/max(stress);
strain=data(6).strain; %change index to change dogbone if necessary
p_initial=[50 25 5 .5]*10e-3;
a0=[1 0 0];
%vincoli su zeta imposti da dong ( zeta tra 0 e 1 )
lb=[0 0 0 0]; %% AGGIUSTA UNITA DIN MISURA
ub=[650 98 35 1];
%ottimizzazione non lineare con metodo least square ( like Dong )
options = optimoptions(‘ga’, ‘Display’, ‘iter’, ‘PopulationSize’, 100, ‘MaxGenerations’, 200, ‘UseParallel’, true,’NonlinearConstraintAlgorithm’,’penalty’,’PlotFcn’,’gaplotbestindiv’,’InitialPopulationMatrix’,p_initial);
nvars=4;
[p_opt, fval, exitflag, output] = ga(@(p)(error_function(p, strain, stress)), nvars, [], [], [], [], lb, ub, [], options);
%check plot e ricostruzione del modello con i parametri ottimizzati
c = p_opt(1); % c
k1 = p_opt(2); % k1
k2 = p_opt(3); % k2
zeta = p_opt(4); % zeta
for i=1:length(strain)
sigma_calculated(i)=c*(strain(i)^2) + 2*strain(i)^2*k1*((strain(i)^2-1)*(i-zeta)^2*exp(k2*(strain(i)^2-1)^2*(1-zeta)^2));
end
% for i=1:length(strain)
% f = [strain(i) 0 0; %deformation gradient for each value of strain
% 0 1/sqrt(strain(i)) 0;
% 0 0 1/sqrt(strain(i))];
% C = f .* f’; % Calcola il tensore di Cauchy-Green
% prodot = a0 .* a0′;
% I4 = sum(sum(C .* prodot)); % calcolo invariante di C
% I = eye(3); % Matrice identità
%
% sigma= f*c*I*f’ + 2*f*(k1 * (1 – zeta)^2 * (I4 – 1) * (exp(k2 * ((1 – zeta) * (I4 – 1))^2)) * prodot)*f’;
%
% sigma_calculated(i)= sigma(1,1); %componente xx
% end
figure()
plot (strain,stress);
hold on
plot(strain, sigma_calculated,’r’);
legend (‘experimental’, ‘model’,’Location’, ‘northwest’);
function error = error_function(p,strain,stress)
sigma_calculated = zeros(length(strain), 1);
for i = 2:length(strain) %avoiding first value=0;
% f = [strain(i) 0 0; %deformation gradient
% 0 1/sqrt(strain(i)) 0;
% 0 0 1/sqrt(strain(i))];
% C = f .* f’; % tensore di Cauchy-Green
% prodot = a0 .* a0′;
% I4 = a0′ .* C .* a0; % calcolo IV invariante di C
% I = eye(3);
%
% sigma= f*p(1)*I*f’ + 2*f*(p(2) * (1 – p(4))^2 * (I4 – 1) * (exp(p(3) * ((1 – p(4)) * (I4 – 1))^2)) * prodot)*f’;
%
%
% sigma_calculated(i)= sigma(1,1); %componente xx
sigma_calculated(i)=p(1)*(strain(i)^2) + 2*strain(i)^2*p(2)*((strain(i)^2-1)*(i-p(4))^2*exp(p(3)*(strain(i)^2-1)^2*(1-p(4))^2));
end
error= sum((sigma_calculated-stress).^2) ; %/sum(stress).^2; % Somma dei quadrati delle differenze
%aggiungere normalizzazione rispetto allo stress( guardare script marta calcolo error )
end I have a constitutive model with 4 parameters to fit to experimental stress vs strain data. Before implementing the GA, I tried lsqnonlin (with and without multistart), but one of the main problems is that the algorithm often tends to assign bound values to the optimised parameters. To the first parameter, which is larger than the others, it assigns the upper bound, to the second and third it assigns the lower bounds. Plotting the best individuals over time, it appears that the ga spans little between the solutions, and the optimisation seems to be biased towards the first parameter.
I am using the uniaxial model proposed by Dong, and have rechecked the correctness of the model form several times. Below is the main containing the ga and my error function. Any help is welcome, thank you
%CHANGE LINE 3 TO SELECT THE PATCH
%CHANGE LINES 9 AND 10 TO SELECT THE SPECIMEN
clc; clear all;close all;
load ("data_11122.mat"); % change lot number if necessary
clearvars -except data
%Model requires lambda= x/x0 instead of epsilon: calculate lambda
% from exp data and store in data struct
stress=data(6).stress; %change index to change dogbone if necessary
stress=stress/max(stress);
strain=data(6).strain; %change index to change dogbone if necessary
p_initial=[50 25 5 .5]*10e-3;
a0=[1 0 0];
%vincoli su zeta imposti da dong ( zeta tra 0 e 1 )
lb=[0 0 0 0]; %% AGGIUSTA UNITA DIN MISURA
ub=[650 98 35 1];
%ottimizzazione non lineare con metodo least square ( like Dong )
options = optimoptions(‘ga’, ‘Display’, ‘iter’, ‘PopulationSize’, 100, ‘MaxGenerations’, 200, ‘UseParallel’, true,’NonlinearConstraintAlgorithm’,’penalty’,’PlotFcn’,’gaplotbestindiv’,’InitialPopulationMatrix’,p_initial);
nvars=4;
[p_opt, fval, exitflag, output] = ga(@(p)(error_function(p, strain, stress)), nvars, [], [], [], [], lb, ub, [], options);
%check plot e ricostruzione del modello con i parametri ottimizzati
c = p_opt(1); % c
k1 = p_opt(2); % k1
k2 = p_opt(3); % k2
zeta = p_opt(4); % zeta
for i=1:length(strain)
sigma_calculated(i)=c*(strain(i)^2) + 2*strain(i)^2*k1*((strain(i)^2-1)*(i-zeta)^2*exp(k2*(strain(i)^2-1)^2*(1-zeta)^2));
end
% for i=1:length(strain)
% f = [strain(i) 0 0; %deformation gradient for each value of strain
% 0 1/sqrt(strain(i)) 0;
% 0 0 1/sqrt(strain(i))];
% C = f .* f’; % Calcola il tensore di Cauchy-Green
% prodot = a0 .* a0′;
% I4 = sum(sum(C .* prodot)); % calcolo invariante di C
% I = eye(3); % Matrice identità
%
% sigma= f*c*I*f’ + 2*f*(k1 * (1 – zeta)^2 * (I4 – 1) * (exp(k2 * ((1 – zeta) * (I4 – 1))^2)) * prodot)*f’;
%
% sigma_calculated(i)= sigma(1,1); %componente xx
% end
figure()
plot (strain,stress);
hold on
plot(strain, sigma_calculated,’r’);
legend (‘experimental’, ‘model’,’Location’, ‘northwest’);
function error = error_function(p,strain,stress)
sigma_calculated = zeros(length(strain), 1);
for i = 2:length(strain) %avoiding first value=0;
% f = [strain(i) 0 0; %deformation gradient
% 0 1/sqrt(strain(i)) 0;
% 0 0 1/sqrt(strain(i))];
% C = f .* f’; % tensore di Cauchy-Green
% prodot = a0 .* a0′;
% I4 = a0′ .* C .* a0; % calcolo IV invariante di C
% I = eye(3);
%
% sigma= f*p(1)*I*f’ + 2*f*(p(2) * (1 – p(4))^2 * (I4 – 1) * (exp(p(3) * ((1 – p(4)) * (I4 – 1))^2)) * prodot)*f’;
%
%
% sigma_calculated(i)= sigma(1,1); %componente xx
sigma_calculated(i)=p(1)*(strain(i)^2) + 2*strain(i)^2*p(2)*((strain(i)^2-1)*(i-p(4))^2*exp(p(3)*(strain(i)^2-1)^2*(1-p(4))^2));
end
error= sum((sigma_calculated-stress).^2) ; %/sum(stress).^2; % Somma dei quadrati delle differenze
%aggiungere normalizzazione rispetto allo stress( guardare script marta calcolo error )
end ga, optimization, nonlinear, curve fitting MATLAB Answers — New Questions
Creating a three-phase UPS in MATLAB
I am having a problem trying to create a Three-phase UPS in MATLAB, I do have an existing file whereby the system works as a single phase. I have then multiplied the system by three and connected it to a load at the output however the output seems to flatline and then rise again, could I get some help on this.
Kind Regards,I am having a problem trying to create a Three-phase UPS in MATLAB, I do have an existing file whereby the system works as a single phase. I have then multiplied the system by three and connected it to a load at the output however the output seems to flatline and then rise again, could I get some help on this.
Kind Regards, I am having a problem trying to create a Three-phase UPS in MATLAB, I do have an existing file whereby the system works as a single phase. I have then multiplied the system by three and connected it to a load at the output however the output seems to flatline and then rise again, could I get some help on this.
Kind Regards, power_conversion_control, ups, three-phase MATLAB Answers — New Questions
geoplot3 for subsurface lines
Dear community,
I fing the geoglobe and geoplot3 functions reallly nice and powerful, yet I have been trying to plot a simple case without succeeding.
I want to plot lines that are located below the surface (underground tunnels, wells, etc.). Is there a workaround to play with the ground layer "transparency" to see those lines that are drawn efectively below, but hidden by the surface?
An extract of the code I’m using:
uif = uifigure;
lat=[43.93365285 43.94086810];
lon=[5.48483510 5.48376465];
alt=[483.591 499.640];
g = geoglobe(uif,’Basemap’,’satellite’,"Terrain","gmted2010");
geoplot3(g,lat,lon,alt,’r’,"LineWidth",2)
campos(g,43.912,5.465,700);
campitch(g,0)
camheading(g,20)
Thanks a lot in advance.Dear community,
I fing the geoglobe and geoplot3 functions reallly nice and powerful, yet I have been trying to plot a simple case without succeeding.
I want to plot lines that are located below the surface (underground tunnels, wells, etc.). Is there a workaround to play with the ground layer "transparency" to see those lines that are drawn efectively below, but hidden by the surface?
An extract of the code I’m using:
uif = uifigure;
lat=[43.93365285 43.94086810];
lon=[5.48483510 5.48376465];
alt=[483.591 499.640];
g = geoglobe(uif,’Basemap’,’satellite’,"Terrain","gmted2010");
geoplot3(g,lat,lon,alt,’r’,"LineWidth",2)
campos(g,43.912,5.465,700);
campitch(g,0)
camheading(g,20)
Thanks a lot in advance. Dear community,
I fing the geoglobe and geoplot3 functions reallly nice and powerful, yet I have been trying to plot a simple case without succeeding.
I want to plot lines that are located below the surface (underground tunnels, wells, etc.). Is there a workaround to play with the ground layer "transparency" to see those lines that are drawn efectively below, but hidden by the surface?
An extract of the code I’m using:
uif = uifigure;
lat=[43.93365285 43.94086810];
lon=[5.48483510 5.48376465];
alt=[483.591 499.640];
g = geoglobe(uif,’Basemap’,’satellite’,"Terrain","gmted2010");
geoplot3(g,lat,lon,alt,’r’,"LineWidth",2)
campos(g,43.912,5.465,700);
campitch(g,0)
camheading(g,20)
Thanks a lot in advance. geoplot3 MATLAB Answers — New Questions
Can I use the same test cases for both a fixed-point and floating-point model in Simulink Test?
I am doing model-in-loop (normal simulation) and software-in-loop testing on my models (codegen). Previously, I have been working with floating-point models and am now moving to work with fixed-point models. I am interested in knowing how Simulink Test supports signal and parameter scaling for fixed-point models in SIL workflows, and whether I need to provide parameter scaling and offsets for floating-point to fixed-point conversion of model inputs and outputs. I am familiar with using A2L files to do parameter scaling and offsets for fixed-point data conversion when running tests at the test bench with SIL workflows with other test tools. The input stimulus and expected outputs for my tests are pulled from an external Excel file.
When Simulink Test is used to manage test cases with an external Excel file to provide the input stimulus and expected outputs, and to manage parameter override, all signals and parameters are also in engineering units. This works OK with MIL testing since model is also in the engineering domain. However, the generated code is in integer world, and the SIL simulation takes integer values for inputs and outputs. What can be done in Simulink Test and/or the Excel file so the same test cases can be re-used for both MIL and SIL? Simulink Test should play the role of the Calibration/Test tool and somehow allow the user to specify the scaling and offset so as to unify both worlds."I am doing model-in-loop (normal simulation) and software-in-loop testing on my models (codegen). Previously, I have been working with floating-point models and am now moving to work with fixed-point models. I am interested in knowing how Simulink Test supports signal and parameter scaling for fixed-point models in SIL workflows, and whether I need to provide parameter scaling and offsets for floating-point to fixed-point conversion of model inputs and outputs. I am familiar with using A2L files to do parameter scaling and offsets for fixed-point data conversion when running tests at the test bench with SIL workflows with other test tools. The input stimulus and expected outputs for my tests are pulled from an external Excel file.
When Simulink Test is used to manage test cases with an external Excel file to provide the input stimulus and expected outputs, and to manage parameter override, all signals and parameters are also in engineering units. This works OK with MIL testing since model is also in the engineering domain. However, the generated code is in integer world, and the SIL simulation takes integer values for inputs and outputs. What can be done in Simulink Test and/or the Excel file so the same test cases can be re-used for both MIL and SIL? Simulink Test should play the role of the Calibration/Test tool and somehow allow the user to specify the scaling and offset so as to unify both worlds." I am doing model-in-loop (normal simulation) and software-in-loop testing on my models (codegen). Previously, I have been working with floating-point models and am now moving to work with fixed-point models. I am interested in knowing how Simulink Test supports signal and parameter scaling for fixed-point models in SIL workflows, and whether I need to provide parameter scaling and offsets for floating-point to fixed-point conversion of model inputs and outputs. I am familiar with using A2L files to do parameter scaling and offsets for fixed-point data conversion when running tests at the test bench with SIL workflows with other test tools. The input stimulus and expected outputs for my tests are pulled from an external Excel file.
When Simulink Test is used to manage test cases with an external Excel file to provide the input stimulus and expected outputs, and to manage parameter override, all signals and parameters are also in engineering units. This works OK with MIL testing since model is also in the engineering domain. However, the generated code is in integer world, and the SIL simulation takes integer values for inputs and outputs. What can be done in Simulink Test and/or the Excel file so the same test cases can be re-used for both MIL and SIL? Simulink Test should play the role of the Calibration/Test tool and somehow allow the user to specify the scaling and offset so as to unify both worlds." sil, mil MATLAB Answers — New Questions