Month: January 2025
Number of arithmetic operations in Simulink model
Is it possible to count the number of multiplications and additions in Simulink model?Is it possible to count the number of multiplications and additions in Simulink model? Is it possible to count the number of multiplications and additions in Simulink model? number of multiplications and additions MATLAB Answers — New Questions
MATLAB in Python problems
Recently I came to learn python to use it in MATLAB from a learn python tutorial. The figure turned off automatically after it showed up. Can anyone tell me whats the probable reason for this?
I wrote a simple code through cmd in windows:
import matlab.engine
eng=matlab.engine.start_matlab()
eng.plot(1.0,2.0,nargout=0)
It works well.
But I wrote exactly the same code in pycharm, the figure turned off automatically after it showed up.Recently I came to learn python to use it in MATLAB from a learn python tutorial. The figure turned off automatically after it showed up. Can anyone tell me whats the probable reason for this?
I wrote a simple code through cmd in windows:
import matlab.engine
eng=matlab.engine.start_matlab()
eng.plot(1.0,2.0,nargout=0)
It works well.
But I wrote exactly the same code in pycharm, the figure turned off automatically after it showed up. Recently I came to learn python to use it in MATLAB from a learn python tutorial. The figure turned off automatically after it showed up. Can anyone tell me whats the probable reason for this?
I wrote a simple code through cmd in windows:
import matlab.engine
eng=matlab.engine.start_matlab()
eng.plot(1.0,2.0,nargout=0)
It works well.
But I wrote exactly the same code in pycharm, the figure turned off automatically after it showed up. python, coding MATLAB Answers — New Questions
How to click and sort a header in uiTable?
I would like to sort the columns in a table by clicking on the column header I want to sort.
Is there a way to click/select the headers and sort the column in uitable?I would like to sort the columns in a table by clicking on the column header I want to sort.
Is there a way to click/select the headers and sort the column in uitable? I would like to sort the columns in a table by clicking on the column header I want to sort.
Is there a way to click/select the headers and sort the column in uitable? uitable, sort MATLAB Answers — New Questions
How to create simulink Data dictionary by using a command in a matlab script ?
I have a Simulink Model block with some entries and outputs.Each Input and output has its Data types.
Apart from this i have an array where in one column i have all the input and output names and on one side i have its corresponding data types (all stored in an array).
From only these data how do i create a simulink data dictionary . How do i choose only this section from the array and create a data dictionary out of it .
For example : ‘MyDataDictionary.sldd’ containing a column named ‘Names’ and the next column named ‘DataTypes’ and all these saved in an sldd format 5all inside amatlab script) .
I want it like an automated script without going into simulink model explorer and then getting the data dictionary.
Any help will be appreciated .I have a Simulink Model block with some entries and outputs.Each Input and output has its Data types.
Apart from this i have an array where in one column i have all the input and output names and on one side i have its corresponding data types (all stored in an array).
From only these data how do i create a simulink data dictionary . How do i choose only this section from the array and create a data dictionary out of it .
For example : ‘MyDataDictionary.sldd’ containing a column named ‘Names’ and the next column named ‘DataTypes’ and all these saved in an sldd format 5all inside amatlab script) .
I want it like an automated script without going into simulink model explorer and then getting the data dictionary.
Any help will be appreciated . I have a Simulink Model block with some entries and outputs.Each Input and output has its Data types.
Apart from this i have an array where in one column i have all the input and output names and on one side i have its corresponding data types (all stored in an array).
From only these data how do i create a simulink data dictionary . How do i choose only this section from the array and create a data dictionary out of it .
For example : ‘MyDataDictionary.sldd’ containing a column named ‘Names’ and the next column named ‘DataTypes’ and all these saved in an sldd format 5all inside amatlab script) .
I want it like an automated script without going into simulink model explorer and then getting the data dictionary.
Any help will be appreciated . matlab, simulink MATLAB Answers — New Questions
How to convert digital numerals (1, 2, 3) into number words (‘one’, ‘two’, ‘three’)?
I have input data like:
1
2
3
4
13
15
23
and I want the output like:
one
two
three
four
thirteen
fifteen
twenty threeI have input data like:
1
2
3
4
13
15
23
and I want the output like:
one
two
three
four
thirteen
fifteen
twenty three I have input data like:
1
2
3
4
13
15
23
and I want the output like:
one
two
three
four
thirteen
fifteen
twenty three convert, text MATLAB Answers — New Questions
error in using fscanf
I have a script like this:
%Inputing POE
file_01=’POE_0.01.txt’;
fid=fopen(file_01, ‘r’);
FileName_01=fscanf(fid,’%g %g %g’, [3,Inf])’;
Mag_01=FileName_01(:,1); % moment magnitude
Dist_01=FileName_01(:,2); % distance
POE_01=FileName_01(:,3); % probability of exceedance
I = (mod(1:length(Mag_01),30)==1);
Mag = Mag_01(I);
Dist = Dist_01(1:30);
POE_01 = reshape(POE_01,30,15);
marginal(1:15,1) = sum(POE_01);
file_POE = [‘POE_0.5.txt’,’POE_0.2.txt’,’POE_0.1.txt’,’POE_0.05.txt’, …
‘POE_0.02.txt’,’POE_0.01.txt’,’POE_0.005.txt’];
for i = 1:7
file_temp=file_POE(i);
fid=fopen(file_temp, ‘r’);
FileName_temp=fscanf(fid,’%2.2f %g %g’, [3,Inf])’;
POE=FileName_temp(:,3); % probability of exceedance
marginal(1:15,i)=sum(reshape(POE,30,15));
temp=sum(marginal);
hold on
plot(Mag,marginal(1:15,i)/temp(i),’DisplayName’,file_POE(i))
xlabel(‘Magnitude (Mw)’);
ylabel(‘Contribution to the Hazard’);
end
xlim([4.5 8.8])
legend
I got a massage about :
Error using fscanf
Invalid file identifier. Use fopen to generate a valid file identifier.
Error in inputing_data (line 73)
FileName_temp=fscanf(fid,’%g %g %g’, [3,Inf
Is there any one can help?I have a script like this:
%Inputing POE
file_01=’POE_0.01.txt’;
fid=fopen(file_01, ‘r’);
FileName_01=fscanf(fid,’%g %g %g’, [3,Inf])’;
Mag_01=FileName_01(:,1); % moment magnitude
Dist_01=FileName_01(:,2); % distance
POE_01=FileName_01(:,3); % probability of exceedance
I = (mod(1:length(Mag_01),30)==1);
Mag = Mag_01(I);
Dist = Dist_01(1:30);
POE_01 = reshape(POE_01,30,15);
marginal(1:15,1) = sum(POE_01);
file_POE = [‘POE_0.5.txt’,’POE_0.2.txt’,’POE_0.1.txt’,’POE_0.05.txt’, …
‘POE_0.02.txt’,’POE_0.01.txt’,’POE_0.005.txt’];
for i = 1:7
file_temp=file_POE(i);
fid=fopen(file_temp, ‘r’);
FileName_temp=fscanf(fid,’%2.2f %g %g’, [3,Inf])’;
POE=FileName_temp(:,3); % probability of exceedance
marginal(1:15,i)=sum(reshape(POE,30,15));
temp=sum(marginal);
hold on
plot(Mag,marginal(1:15,i)/temp(i),’DisplayName’,file_POE(i))
xlabel(‘Magnitude (Mw)’);
ylabel(‘Contribution to the Hazard’);
end
xlim([4.5 8.8])
legend
I got a massage about :
Error using fscanf
Invalid file identifier. Use fopen to generate a valid file identifier.
Error in inputing_data (line 73)
FileName_temp=fscanf(fid,’%g %g %g’, [3,Inf
Is there any one can help? I have a script like this:
%Inputing POE
file_01=’POE_0.01.txt’;
fid=fopen(file_01, ‘r’);
FileName_01=fscanf(fid,’%g %g %g’, [3,Inf])’;
Mag_01=FileName_01(:,1); % moment magnitude
Dist_01=FileName_01(:,2); % distance
POE_01=FileName_01(:,3); % probability of exceedance
I = (mod(1:length(Mag_01),30)==1);
Mag = Mag_01(I);
Dist = Dist_01(1:30);
POE_01 = reshape(POE_01,30,15);
marginal(1:15,1) = sum(POE_01);
file_POE = [‘POE_0.5.txt’,’POE_0.2.txt’,’POE_0.1.txt’,’POE_0.05.txt’, …
‘POE_0.02.txt’,’POE_0.01.txt’,’POE_0.005.txt’];
for i = 1:7
file_temp=file_POE(i);
fid=fopen(file_temp, ‘r’);
FileName_temp=fscanf(fid,’%2.2f %g %g’, [3,Inf])’;
POE=FileName_temp(:,3); % probability of exceedance
marginal(1:15,i)=sum(reshape(POE,30,15));
temp=sum(marginal);
hold on
plot(Mag,marginal(1:15,i)/temp(i),’DisplayName’,file_POE(i))
xlabel(‘Magnitude (Mw)’);
ylabel(‘Contribution to the Hazard’);
end
xlim([4.5 8.8])
legend
I got a massage about :
Error using fscanf
Invalid file identifier. Use fopen to generate a valid file identifier.
Error in inputing_data (line 73)
FileName_temp=fscanf(fid,’%g %g %g’, [3,Inf
Is there any one can help? error input and read data MATLAB Answers — New Questions
How do I convert a .m with unsupported in-built MATLAB functions to C++
Hi everyone.
I’m currently trying to convert a MATLAB script to C++ but MATLAB Coder gives some errors saying some functions are unsupported in the code conversion.
Below are the unsupported functions
fullfile is not supported for code generation
com.mathworks.toolbox.coder.screener.ScreenerError@ca978627
imageSet is not supported for code generation
com.mathworks.toolbox.coder.screener.ScreenerError@36c733bd
partition is not supported for code generation
com.mathworks.toolbox.coder.screener.ScreenerError@b1e1e753
bagOfFeatures is not supported for code generation
com.mathworks.toolbox.coder.screener.ScreenerError@3e7efb02
encode is not supported for code generation
com.mathworks.toolbox.coder.screener.ScreenerError@31cdf3a9
bar is not supported for code generation
com.mathworks.toolbox.coder.screener.ScreenerError@83fd9938Hi everyone.
I’m currently trying to convert a MATLAB script to C++ but MATLAB Coder gives some errors saying some functions are unsupported in the code conversion.
Below are the unsupported functions
fullfile is not supported for code generation
com.mathworks.toolbox.coder.screener.ScreenerError@ca978627
imageSet is not supported for code generation
com.mathworks.toolbox.coder.screener.ScreenerError@36c733bd
partition is not supported for code generation
com.mathworks.toolbox.coder.screener.ScreenerError@b1e1e753
bagOfFeatures is not supported for code generation
com.mathworks.toolbox.coder.screener.ScreenerError@3e7efb02
encode is not supported for code generation
com.mathworks.toolbox.coder.screener.ScreenerError@31cdf3a9
bar is not supported for code generation
com.mathworks.toolbox.coder.screener.ScreenerError@83fd9938 Hi everyone.
I’m currently trying to convert a MATLAB script to C++ but MATLAB Coder gives some errors saying some functions are unsupported in the code conversion.
Below are the unsupported functions
fullfile is not supported for code generation
com.mathworks.toolbox.coder.screener.ScreenerError@ca978627
imageSet is not supported for code generation
com.mathworks.toolbox.coder.screener.ScreenerError@36c733bd
partition is not supported for code generation
com.mathworks.toolbox.coder.screener.ScreenerError@b1e1e753
bagOfFeatures is not supported for code generation
com.mathworks.toolbox.coder.screener.ScreenerError@3e7efb02
encode is not supported for code generation
com.mathworks.toolbox.coder.screener.ScreenerError@31cdf3a9
bar is not supported for code generation
com.mathworks.toolbox.coder.screener.ScreenerError@83fd9938 matlab coder, coder, fullfile MATLAB Answers — New Questions
Looking for help in using structs in multiple functions
I am trying to use structs, but I have no idea what a 0x1 struct is, and I am trying to add to the struct outside of a function, but I don’t understand how they work. None of the websites have a good explanation of how to use the struct. Plus, I get an error when I use the dot method to add to it.I am trying to use structs, but I have no idea what a 0x1 struct is, and I am trying to add to the struct outside of a function, but I don’t understand how they work. None of the websites have a good explanation of how to use the struct. Plus, I get an error when I use the dot method to add to it. I am trying to use structs, but I have no idea what a 0x1 struct is, and I am trying to add to the struct outside of a function, but I don’t understand how they work. None of the websites have a good explanation of how to use the struct. Plus, I get an error when I use the dot method to add to it. structs MATLAB Answers — New Questions
Modeling of cooling plate
Hello Team,
I am using simscape foundation library to model side cooling of a cylidrical cell which is represented by a thermal mass and the heat generation in each cell is represented by heat source.
There are two plates on each cell, the top plate and the bottom plate. the bottom plate is where the fluid is entering at certain flow rate, temperature and pressure. and after passing through all the bottom plates for a series of cells, the fluid enters the top plate and then exits the system.
I am currently modeling it in simscape using foundation library and not able to simulate the model, as I am not able to provide flow rate, temperatrure and pressure properly and cannot implement closed loop system.
Please help me in modeling thisHello Team,
I am using simscape foundation library to model side cooling of a cylidrical cell which is represented by a thermal mass and the heat generation in each cell is represented by heat source.
There are two plates on each cell, the top plate and the bottom plate. the bottom plate is where the fluid is entering at certain flow rate, temperature and pressure. and after passing through all the bottom plates for a series of cells, the fluid enters the top plate and then exits the system.
I am currently modeling it in simscape using foundation library and not able to simulate the model, as I am not able to provide flow rate, temperatrure and pressure properly and cannot implement closed loop system.
Please help me in modeling this Hello Team,
I am using simscape foundation library to model side cooling of a cylidrical cell which is represented by a thermal mass and the heat generation in each cell is represented by heat source.
There are two plates on each cell, the top plate and the bottom plate. the bottom plate is where the fluid is entering at certain flow rate, temperature and pressure. and after passing through all the bottom plates for a series of cells, the fluid enters the top plate and then exits the system.
I am currently modeling it in simscape using foundation library and not able to simulate the model, as I am not able to provide flow rate, temperatrure and pressure properly and cannot implement closed loop system.
Please help me in modeling this cooling plate, battery_system_management, thermal, pipe, pressure, temperature, flow rate, thermal fluid MATLAB Answers — New Questions
i have a problem on Enumerations data type
how to create a script that changes the name of enumeration variables in an sldd filehow to create a script that changes the name of enumeration variables in an sldd file how to create a script that changes the name of enumeration variables in an sldd file sldd datadictionary MATLAB Answers — New Questions
Combining structs into one struct.
Hello, I have several structs that I would like to combine together into one. The issue I find is that the data for some reasons gets completely jumbled. Each time the file runs, the columns from one struct become a part of another and I cannot figure out why
CombineGLMParam(‘2_2_2’, ‘2_3_2′,’2’)
CombineGLMParam(0, ‘2_5_1′,’2’)
function CombineGLMParam(bat1,bat2,bat)
%Combine GLM parameters by putting in the bat and the file. If bat1 is not 0, open the bat1 file and create a file GLM bat.
% If it is 0, open the GLM Bat file and append the bat 2 file to GLM.
if bat1~=0
b1=load([dirf,’Bat_’,bat1,’_GLM_Params.mat’]);
b2=load([dirf,’Bat_’,bat2,’_GLM_Params.mat’]);
b1=struct2cell(b1)
b2=struct2cell(b2)
else
b1=load([dirf,’GLM_’,bat,’.mat’]);
b2=load([dirf,’Bat_’,bat2,’_GLM_Params.mat’]);
b1=struct2cell(b1.b4)
b2=struct2cell(b2)
end
for n=1:size(b2,1)
b3{n}=[b1{n}’ b2{n}’]’;
end
Header={‘ThetaX’, ‘ThetaY’, ‘ThetaZ’, ‘Td’, ‘Vd’, ‘Vw’, ‘Hd’, ‘Hw’, ‘Ld’,’Lw’};
b4=cell2struct(b3, Header, 2)
save([dirf,’GLM_’,bat,’.mat’], ‘b4’)Hello, I have several structs that I would like to combine together into one. The issue I find is that the data for some reasons gets completely jumbled. Each time the file runs, the columns from one struct become a part of another and I cannot figure out why
CombineGLMParam(‘2_2_2’, ‘2_3_2′,’2’)
CombineGLMParam(0, ‘2_5_1′,’2’)
function CombineGLMParam(bat1,bat2,bat)
%Combine GLM parameters by putting in the bat and the file. If bat1 is not 0, open the bat1 file and create a file GLM bat.
% If it is 0, open the GLM Bat file and append the bat 2 file to GLM.
if bat1~=0
b1=load([dirf,’Bat_’,bat1,’_GLM_Params.mat’]);
b2=load([dirf,’Bat_’,bat2,’_GLM_Params.mat’]);
b1=struct2cell(b1)
b2=struct2cell(b2)
else
b1=load([dirf,’GLM_’,bat,’.mat’]);
b2=load([dirf,’Bat_’,bat2,’_GLM_Params.mat’]);
b1=struct2cell(b1.b4)
b2=struct2cell(b2)
end
for n=1:size(b2,1)
b3{n}=[b1{n}’ b2{n}’]’;
end
Header={‘ThetaX’, ‘ThetaY’, ‘ThetaZ’, ‘Td’, ‘Vd’, ‘Vw’, ‘Hd’, ‘Hw’, ‘Ld’,’Lw’};
b4=cell2struct(b3, Header, 2)
save([dirf,’GLM_’,bat,’.mat’], ‘b4’) Hello, I have several structs that I would like to combine together into one. The issue I find is that the data for some reasons gets completely jumbled. Each time the file runs, the columns from one struct become a part of another and I cannot figure out why
CombineGLMParam(‘2_2_2’, ‘2_3_2′,’2’)
CombineGLMParam(0, ‘2_5_1′,’2’)
function CombineGLMParam(bat1,bat2,bat)
%Combine GLM parameters by putting in the bat and the file. If bat1 is not 0, open the bat1 file and create a file GLM bat.
% If it is 0, open the GLM Bat file and append the bat 2 file to GLM.
if bat1~=0
b1=load([dirf,’Bat_’,bat1,’_GLM_Params.mat’]);
b2=load([dirf,’Bat_’,bat2,’_GLM_Params.mat’]);
b1=struct2cell(b1)
b2=struct2cell(b2)
else
b1=load([dirf,’GLM_’,bat,’.mat’]);
b2=load([dirf,’Bat_’,bat2,’_GLM_Params.mat’]);
b1=struct2cell(b1.b4)
b2=struct2cell(b2)
end
for n=1:size(b2,1)
b3{n}=[b1{n}’ b2{n}’]’;
end
Header={‘ThetaX’, ‘ThetaY’, ‘ThetaZ’, ‘Td’, ‘Vd’, ‘Vw’, ‘Hd’, ‘Hw’, ‘Ld’,’Lw’};
b4=cell2struct(b3, Header, 2)
save([dirf,’GLM_’,bat,’.mat’], ‘b4’) struct, structure MATLAB Answers — New Questions
How to set Simulink block parameters using an absolute model path
I am calling a simulink model out of a MATLAB script, but this model is located in a different directory (not in the working directory).
This model can be executed by using the "sim" command in combination with the absolute path, e.g:
sim("C:UsersMATLABmyModel.slx")
Now I want to change the parameter of a "Contant" block when calling the simulation.
The functions "set_param" seems only to work with a model handle which was created by a model being in the current working path.
% name of model
mdl = "myMdl";
% name of the block
blk = "myConstBlk";
% Serch path
path = strcat(mdl, "/", blk);
% set parameter of Const
set_param(path, "Value", "5");
When performing the same action, but with an absolute path, this is not working anymore.
% name of model
mdl = "C:UsersMATLABmyModel.slx";
% name of the block
blk = "myConstBlk";
% Serch path
path = strcat(mdl, "/", blk);
% set parameter of Const
set_param(path, "Value", "5");
Invalid Simulink object name: ‘C:UsersMATLABmyMdl.slx/myConstBlk’.
Even if the model is loaded by:
h = load_system(mdl); %mdl is the absolute path
the handel "h" could not be used in the set_param function.
So therefore the qustion:
How to perform all the task provided by "set_param" and "get_param" using a model which shall be referenced by an absolute path?I am calling a simulink model out of a MATLAB script, but this model is located in a different directory (not in the working directory).
This model can be executed by using the "sim" command in combination with the absolute path, e.g:
sim("C:UsersMATLABmyModel.slx")
Now I want to change the parameter of a "Contant" block when calling the simulation.
The functions "set_param" seems only to work with a model handle which was created by a model being in the current working path.
% name of model
mdl = "myMdl";
% name of the block
blk = "myConstBlk";
% Serch path
path = strcat(mdl, "/", blk);
% set parameter of Const
set_param(path, "Value", "5");
When performing the same action, but with an absolute path, this is not working anymore.
% name of model
mdl = "C:UsersMATLABmyModel.slx";
% name of the block
blk = "myConstBlk";
% Serch path
path = strcat(mdl, "/", blk);
% set parameter of Const
set_param(path, "Value", "5");
Invalid Simulink object name: ‘C:UsersMATLABmyMdl.slx/myConstBlk’.
Even if the model is loaded by:
h = load_system(mdl); %mdl is the absolute path
the handel "h" could not be used in the set_param function.
So therefore the qustion:
How to perform all the task provided by "set_param" and "get_param" using a model which shall be referenced by an absolute path? I am calling a simulink model out of a MATLAB script, but this model is located in a different directory (not in the working directory).
This model can be executed by using the "sim" command in combination with the absolute path, e.g:
sim("C:UsersMATLABmyModel.slx")
Now I want to change the parameter of a "Contant" block when calling the simulation.
The functions "set_param" seems only to work with a model handle which was created by a model being in the current working path.
% name of model
mdl = "myMdl";
% name of the block
blk = "myConstBlk";
% Serch path
path = strcat(mdl, "/", blk);
% set parameter of Const
set_param(path, "Value", "5");
When performing the same action, but with an absolute path, this is not working anymore.
% name of model
mdl = "C:UsersMATLABmyModel.slx";
% name of the block
blk = "myConstBlk";
% Serch path
path = strcat(mdl, "/", blk);
% set parameter of Const
set_param(path, "Value", "5");
Invalid Simulink object name: ‘C:UsersMATLABmyMdl.slx/myConstBlk’.
Even if the model is loaded by:
h = load_system(mdl); %mdl is the absolute path
the handel "h" could not be used in the set_param function.
So therefore the qustion:
How to perform all the task provided by "set_param" and "get_param" using a model which shall be referenced by an absolute path? absolute path, simulink, set block parameter MATLAB Answers — New Questions
Can I use knobs to control several elements from the same injected struct?
I am accessing an injected struct that has several elements I want to use individual knobs to control through a mask but I am not seeing the elements when I try to connect the knobs to the constant block. Can anyone assist me?I am accessing an injected struct that has several elements I want to use individual knobs to control through a mask but I am not seeing the elements when I try to connect the knobs to the constant block. Can anyone assist me? I am accessing an injected struct that has several elements I want to use individual knobs to control through a mask but I am not seeing the elements when I try to connect the knobs to the constant block. Can anyone assist me? knob, struct, injected, mask MATLAB Answers — New Questions
Monte Carlo integration (hit or miss) to find the area of a circle of radius R
Hi everyone. Trying to solve an old exam topic regarding Monte Carlo integration, I wrote the following code for which I based it on a code from a professor in the C language. For R = 1 it worked so I assume I did it correctly. Then I tried R > 1 and the results were wrong compared to the formula πR^2 so I tried a few modifications by trial and error method. I fixed the problem and it seems to works correctly but there is a modification that I don’t understand. If you could help me understand this it would be great. Thank you in advance.
clear, clc, format short
%———- Computation for R = 1 ———-
R = 1;
N = 100000;
insideCircle = 0;
for i = 1:N
x = rand(1);
y = rand(1);
if (x^2 + y^2) < R
insideCircle = insideCircle + 1;
end
end
Area_MC = 4*insideCircle/N;
Area = pi*R^2;
error = 100*abs(Area – Area_MC)/Area;
fprintf(‘ntExact areatt%fntMonte Carlo areat%fntPercentage errort%.2f%%n’,Area,Area_MC,error);
%———- Computation for R > 1 (e.g. R = 4 but you can try any real value) ———-
R = 4;
N = 100000;
insideCircle = 0;
for i = 1:N
x = rand(1)*R;
y = rand(1)*R;
if (x^2 + y^2) < R
insideCircle = insideCircle + 1;
end
end
Area_MC = 4*R^3*insideCircle/N; % Modification needed that I don’t understand (I had to multiply with R^3)
Area = pi*R^2;
error = 100*abs(Area – Area_MC)/Area;
fprintf(‘ntExact areatt%fntMonte Carlo areat%fntPercentage errort%.2f%%n’,Area,Area_MC,error);Hi everyone. Trying to solve an old exam topic regarding Monte Carlo integration, I wrote the following code for which I based it on a code from a professor in the C language. For R = 1 it worked so I assume I did it correctly. Then I tried R > 1 and the results were wrong compared to the formula πR^2 so I tried a few modifications by trial and error method. I fixed the problem and it seems to works correctly but there is a modification that I don’t understand. If you could help me understand this it would be great. Thank you in advance.
clear, clc, format short
%———- Computation for R = 1 ———-
R = 1;
N = 100000;
insideCircle = 0;
for i = 1:N
x = rand(1);
y = rand(1);
if (x^2 + y^2) < R
insideCircle = insideCircle + 1;
end
end
Area_MC = 4*insideCircle/N;
Area = pi*R^2;
error = 100*abs(Area – Area_MC)/Area;
fprintf(‘ntExact areatt%fntMonte Carlo areat%fntPercentage errort%.2f%%n’,Area,Area_MC,error);
%———- Computation for R > 1 (e.g. R = 4 but you can try any real value) ———-
R = 4;
N = 100000;
insideCircle = 0;
for i = 1:N
x = rand(1)*R;
y = rand(1)*R;
if (x^2 + y^2) < R
insideCircle = insideCircle + 1;
end
end
Area_MC = 4*R^3*insideCircle/N; % Modification needed that I don’t understand (I had to multiply with R^3)
Area = pi*R^2;
error = 100*abs(Area – Area_MC)/Area;
fprintf(‘ntExact areatt%fntMonte Carlo areat%fntPercentage errort%.2f%%n’,Area,Area_MC,error); Hi everyone. Trying to solve an old exam topic regarding Monte Carlo integration, I wrote the following code for which I based it on a code from a professor in the C language. For R = 1 it worked so I assume I did it correctly. Then I tried R > 1 and the results were wrong compared to the formula πR^2 so I tried a few modifications by trial and error method. I fixed the problem and it seems to works correctly but there is a modification that I don’t understand. If you could help me understand this it would be great. Thank you in advance.
clear, clc, format short
%———- Computation for R = 1 ———-
R = 1;
N = 100000;
insideCircle = 0;
for i = 1:N
x = rand(1);
y = rand(1);
if (x^2 + y^2) < R
insideCircle = insideCircle + 1;
end
end
Area_MC = 4*insideCircle/N;
Area = pi*R^2;
error = 100*abs(Area – Area_MC)/Area;
fprintf(‘ntExact areatt%fntMonte Carlo areat%fntPercentage errort%.2f%%n’,Area,Area_MC,error);
%———- Computation for R > 1 (e.g. R = 4 but you can try any real value) ———-
R = 4;
N = 100000;
insideCircle = 0;
for i = 1:N
x = rand(1)*R;
y = rand(1)*R;
if (x^2 + y^2) < R
insideCircle = insideCircle + 1;
end
end
Area_MC = 4*R^3*insideCircle/N; % Modification needed that I don’t understand (I had to multiply with R^3)
Area = pi*R^2;
error = 100*abs(Area – Area_MC)/Area;
fprintf(‘ntExact areatt%fntMonte Carlo areat%fntPercentage errort%.2f%%n’,Area,Area_MC,error); monte carlo integration, hit or miss, area of a cirlcle MATLAB Answers — New Questions
Understanding basics of struct?
Hello,
A beginners short story with 3 and a half Questions:
I want to get a list of all filepath of filenames in a folder (?).
For that I’m using dir which creates a struct (so far so good).
Now I want to save the first colum with the filenames as a cell array (?)
Solved this by using struct2cell .
But is there a way to export a single colum (or even a defined range of that colum) ?
I was also thinking an easy for loop would solve that but…
I’m struggeling to get the length of the struct ?
And…
Is there an elegant way to sort names in the struct in a "natural way" (1,2,3,…,11,12 instead of 1,11,12,2,3,…) ?
Big Thanks!!!Hello,
A beginners short story with 3 and a half Questions:
I want to get a list of all filepath of filenames in a folder (?).
For that I’m using dir which creates a struct (so far so good).
Now I want to save the first colum with the filenames as a cell array (?)
Solved this by using struct2cell .
But is there a way to export a single colum (or even a defined range of that colum) ?
I was also thinking an easy for loop would solve that but…
I’m struggeling to get the length of the struct ?
And…
Is there an elegant way to sort names in the struct in a "natural way" (1,2,3,…,11,12 instead of 1,11,12,2,3,…) ?
Big Thanks!!! Hello,
A beginners short story with 3 and a half Questions:
I want to get a list of all filepath of filenames in a folder (?).
For that I’m using dir which creates a struct (so far so good).
Now I want to save the first colum with the filenames as a cell array (?)
Solved this by using struct2cell .
But is there a way to export a single colum (or even a defined range of that colum) ?
I was also thinking an easy for loop would solve that but…
I’m struggeling to get the length of the struct ?
And…
Is there an elegant way to sort names in the struct in a "natural way" (1,2,3,…,11,12 instead of 1,11,12,2,3,…) ?
Big Thanks!!! struct, filepath, struct2cell, struct length, struct sort, basics MATLAB Answers — New Questions
Mathlab Code for summation equation
Hi all,
How can I write matlab code for summation equation for the file attached.
Pls help.
Thank you guys..Hi all,
How can I write matlab code for summation equation for the file attached.
Pls help.
Thank you guys.. Hi all,
How can I write matlab code for summation equation for the file attached.
Pls help.
Thank you guys.. summation MATLAB Answers — New Questions
How do I write a .mat file (exported from the Design Data in an .sldd file) to a csv file?
I am trying to write the contents of a .mat file to a csv file. The .mat file was generated by exporting the Design Data from a simulink data dictionary file that is used in a Simulink model. I’ve tried to research how to extract the data from the .mat but haven’t been able to find anything significant. If you have any suggestions for strategies or functions I could use, please add a comment. Thanks.
CODE:
FileData = load(‘SLDD_220817.mat’);
NewData=struct2cell(FileData);
csvwrite(‘SLDD_220817.csv’, NewData);
CW OUTPUT:
Error using csvwrite (line 47)
The input cell array cannot be converted to a matrix.
Error in sldd_to_csv (line 11)
csvwrite(‘SLDD_220817.csv’, NewData);I am trying to write the contents of a .mat file to a csv file. The .mat file was generated by exporting the Design Data from a simulink data dictionary file that is used in a Simulink model. I’ve tried to research how to extract the data from the .mat but haven’t been able to find anything significant. If you have any suggestions for strategies or functions I could use, please add a comment. Thanks.
CODE:
FileData = load(‘SLDD_220817.mat’);
NewData=struct2cell(FileData);
csvwrite(‘SLDD_220817.csv’, NewData);
CW OUTPUT:
Error using csvwrite (line 47)
The input cell array cannot be converted to a matrix.
Error in sldd_to_csv (line 11)
csvwrite(‘SLDD_220817.csv’, NewData); I am trying to write the contents of a .mat file to a csv file. The .mat file was generated by exporting the Design Data from a simulink data dictionary file that is used in a Simulink model. I’ve tried to research how to extract the data from the .mat but haven’t been able to find anything significant. If you have any suggestions for strategies or functions I could use, please add a comment. Thanks.
CODE:
FileData = load(‘SLDD_220817.mat’);
NewData=struct2cell(FileData);
csvwrite(‘SLDD_220817.csv’, NewData);
CW OUTPUT:
Error using csvwrite (line 47)
The input cell array cannot be converted to a matrix.
Error in sldd_to_csv (line 11)
csvwrite(‘SLDD_220817.csv’, NewData); sldd, csvwrite MATLAB Answers — New Questions
how to changes for two parameters values in simulink block at same time in matlab 2022. this function is already founded in matlab 2016
Freq=0;F=0; n=0;A=0;
for k=1:20 Freq=Freq+50; n=n+1;
sim(‘Rc_model22’);
if k==1 t=Vcamp(:,1);Vcamp1(:,1)=Vcamp(:,2); AVc1(:,1)=AVc(:,2); end;
if k==2 Vcamp2(:,1)=Vcamp(:,2); AVc2(:,1)=AVc(:,2);end;
if k==3 Vcamp3(:,1)=Vcamp(:,2); AVc3(:,1)=AVc(:,2);end;
if k==4 Vcamp4(:,1)=Vcamp(:,2); AVc4(:,1)=AVc(:,2);end;
if k==5 Vcamp5(:,1)=Vcamp(:,2); AVc5(:,1)=AVc(:,2);end;
if k==6 Vcamp6(:,1)=Vcamp(:,2); AVc6(:,1)=AVc(:,2);end;
if k==7 Vcamp7(:,1)=Vcamp(:,2); AVc7(:,1)=AVc(:,2);end;
if k==8 Vcamp8(:,1)=Vcamp(:,2); AVc8(:,1)=AVc(:,2);end;
if k==9 Vcamp9(:,1)=Vcamp(:,2); AVc9(:,1)=AVc(:,2);end;
if k==10 Vcamp10(:,1)=Vcamp(:,2); AVc10(:,1)=AVc(:,2);end;
if k==11 Vcamp11(:,1)=Vcamp(:,2); AVc11(:,1)=AVc(:,2);end;
if k==12 Vcamp12(:,1)=Vcamp(:,2); AVc12(:,1)=AVc(:,2);end;
if k==13 Vcamp13(:,1)=Vcamp(:,2); AVc13(:,1)=AVc(:,2);end;
if k==14 Vcamp14(:,1)=Vcamp(:,2); AVc14(:,1)=AVc(:,2);end;
if k==15 Vcamp15(:,1)=Vcamp(:,2); AVc15(:,1)=AVc(:,2);end;
if k==16 Vcamp16(:,1)=Vcamp(:,2); AVc16(:,1)=AVc(:,2);end;
if k==17 Vcamp17(:,1)=Vcamp(:,2); AVc17(:,1)=AVc(:,2);end;
if k==18 Vcamp18(:,1)=Vcamp(:,2); AVc18(:,1)=AVc(:,2);end;
if k==19 Vcamp19(:,1)=Vcamp(:,2); AVc19(:,1)=AVc(:,2);end;
if k==20 Vcamp20(:,1)=Vcamp(:,2); AVc20(:,1)=AVc(:,2);end;
endFreq=0;F=0; n=0;A=0;
for k=1:20 Freq=Freq+50; n=n+1;
sim(‘Rc_model22’);
if k==1 t=Vcamp(:,1);Vcamp1(:,1)=Vcamp(:,2); AVc1(:,1)=AVc(:,2); end;
if k==2 Vcamp2(:,1)=Vcamp(:,2); AVc2(:,1)=AVc(:,2);end;
if k==3 Vcamp3(:,1)=Vcamp(:,2); AVc3(:,1)=AVc(:,2);end;
if k==4 Vcamp4(:,1)=Vcamp(:,2); AVc4(:,1)=AVc(:,2);end;
if k==5 Vcamp5(:,1)=Vcamp(:,2); AVc5(:,1)=AVc(:,2);end;
if k==6 Vcamp6(:,1)=Vcamp(:,2); AVc6(:,1)=AVc(:,2);end;
if k==7 Vcamp7(:,1)=Vcamp(:,2); AVc7(:,1)=AVc(:,2);end;
if k==8 Vcamp8(:,1)=Vcamp(:,2); AVc8(:,1)=AVc(:,2);end;
if k==9 Vcamp9(:,1)=Vcamp(:,2); AVc9(:,1)=AVc(:,2);end;
if k==10 Vcamp10(:,1)=Vcamp(:,2); AVc10(:,1)=AVc(:,2);end;
if k==11 Vcamp11(:,1)=Vcamp(:,2); AVc11(:,1)=AVc(:,2);end;
if k==12 Vcamp12(:,1)=Vcamp(:,2); AVc12(:,1)=AVc(:,2);end;
if k==13 Vcamp13(:,1)=Vcamp(:,2); AVc13(:,1)=AVc(:,2);end;
if k==14 Vcamp14(:,1)=Vcamp(:,2); AVc14(:,1)=AVc(:,2);end;
if k==15 Vcamp15(:,1)=Vcamp(:,2); AVc15(:,1)=AVc(:,2);end;
if k==16 Vcamp16(:,1)=Vcamp(:,2); AVc16(:,1)=AVc(:,2);end;
if k==17 Vcamp17(:,1)=Vcamp(:,2); AVc17(:,1)=AVc(:,2);end;
if k==18 Vcamp18(:,1)=Vcamp(:,2); AVc18(:,1)=AVc(:,2);end;
if k==19 Vcamp19(:,1)=Vcamp(:,2); AVc19(:,1)=AVc(:,2);end;
if k==20 Vcamp20(:,1)=Vcamp(:,2); AVc20(:,1)=AVc(:,2);end;
end Freq=0;F=0; n=0;A=0;
for k=1:20 Freq=Freq+50; n=n+1;
sim(‘Rc_model22’);
if k==1 t=Vcamp(:,1);Vcamp1(:,1)=Vcamp(:,2); AVc1(:,1)=AVc(:,2); end;
if k==2 Vcamp2(:,1)=Vcamp(:,2); AVc2(:,1)=AVc(:,2);end;
if k==3 Vcamp3(:,1)=Vcamp(:,2); AVc3(:,1)=AVc(:,2);end;
if k==4 Vcamp4(:,1)=Vcamp(:,2); AVc4(:,1)=AVc(:,2);end;
if k==5 Vcamp5(:,1)=Vcamp(:,2); AVc5(:,1)=AVc(:,2);end;
if k==6 Vcamp6(:,1)=Vcamp(:,2); AVc6(:,1)=AVc(:,2);end;
if k==7 Vcamp7(:,1)=Vcamp(:,2); AVc7(:,1)=AVc(:,2);end;
if k==8 Vcamp8(:,1)=Vcamp(:,2); AVc8(:,1)=AVc(:,2);end;
if k==9 Vcamp9(:,1)=Vcamp(:,2); AVc9(:,1)=AVc(:,2);end;
if k==10 Vcamp10(:,1)=Vcamp(:,2); AVc10(:,1)=AVc(:,2);end;
if k==11 Vcamp11(:,1)=Vcamp(:,2); AVc11(:,1)=AVc(:,2);end;
if k==12 Vcamp12(:,1)=Vcamp(:,2); AVc12(:,1)=AVc(:,2);end;
if k==13 Vcamp13(:,1)=Vcamp(:,2); AVc13(:,1)=AVc(:,2);end;
if k==14 Vcamp14(:,1)=Vcamp(:,2); AVc14(:,1)=AVc(:,2);end;
if k==15 Vcamp15(:,1)=Vcamp(:,2); AVc15(:,1)=AVc(:,2);end;
if k==16 Vcamp16(:,1)=Vcamp(:,2); AVc16(:,1)=AVc(:,2);end;
if k==17 Vcamp17(:,1)=Vcamp(:,2); AVc17(:,1)=AVc(:,2);end;
if k==18 Vcamp18(:,1)=Vcamp(:,2); AVc18(:,1)=AVc(:,2);end;
if k==19 Vcamp19(:,1)=Vcamp(:,2); AVc19(:,1)=AVc(:,2);end;
if k==20 Vcamp20(:,1)=Vcamp(:,2); AVc20(:,1)=AVc(:,2);end;
end how to changes for two parameters values in simuli MATLAB Answers — New Questions
How to sum the product between a matrix and 2 vectors
I need to solve a system of differential equation written as:
For each equation I should solve a summation like:
where K_Br is a matrix previously calculated and n is a vector as the solution of the differential equation system.
For example, for i=3:
I wrote the system of differential equation as:
function ndot = System_ni (t,n)
ndot = zeros(M,1);
ndot(1) = -n(1)*sum(K_Br(1,1:M)*n(1:M));
for i = 2:M
ndot(i) = 1/2*sum(K_Br(1:i-1,i-1:1)*n(i-1:1)*n(1:i-1))-n(i)*sum(K_Br(i,1:M)*n(1:M));
end
end
but summation seems not to work.
Thank you in advance for any help.I need to solve a system of differential equation written as:
For each equation I should solve a summation like:
where K_Br is a matrix previously calculated and n is a vector as the solution of the differential equation system.
For example, for i=3:
I wrote the system of differential equation as:
function ndot = System_ni (t,n)
ndot = zeros(M,1);
ndot(1) = -n(1)*sum(K_Br(1,1:M)*n(1:M));
for i = 2:M
ndot(i) = 1/2*sum(K_Br(1:i-1,i-1:1)*n(i-1:1)*n(1:i-1))-n(i)*sum(K_Br(i,1:M)*n(1:M));
end
end
but summation seems not to work.
Thank you in advance for any help. I need to solve a system of differential equation written as:
For each equation I should solve a summation like:
where K_Br is a matrix previously calculated and n is a vector as the solution of the differential equation system.
For example, for i=3:
I wrote the system of differential equation as:
function ndot = System_ni (t,n)
ndot = zeros(M,1);
ndot(1) = -n(1)*sum(K_Br(1,1:M)*n(1:M));
for i = 2:M
ndot(i) = 1/2*sum(K_Br(1:i-1,i-1:1)*n(i-1:1)*n(1:i-1))-n(i)*sum(K_Br(i,1:M)*n(1:M));
end
end
but summation seems not to work.
Thank you in advance for any help. summation, product, matrix vector product, differential equations MATLAB Answers — New Questions
The following code is correct?
I have a Ph of size 139 48 101 values ranging say 0 to 0.4, now to compute Kd from Ph have two equations if Ph is equal or less than 0.35 and if Ph is greater than 0.35. I used following code to apply two equation. Have I done it correctly?
Kd = zeros(size(Ph));
% Find indices where Ph is less than or equal to 0.35
indices1 = Ph <= 0.35;
% Find indices where Ph is greater than 0.35
indices2 = Ph > 0.35;
% Apply the first equation to ‘kd’ where PhiE is less than or equal to 0.35
Kd(indices1) = 38.18.*(1-3.39.*Ph(indices1)+1.95.*Ph(indices1).^2).*1e9;
% Apply the second equation to ‘kd’ where Ph is greater than 0.35
Kd(indices2) = exp(-62.60.*PhiE(indices2)+22.58).*1e9;I have a Ph of size 139 48 101 values ranging say 0 to 0.4, now to compute Kd from Ph have two equations if Ph is equal or less than 0.35 and if Ph is greater than 0.35. I used following code to apply two equation. Have I done it correctly?
Kd = zeros(size(Ph));
% Find indices where Ph is less than or equal to 0.35
indices1 = Ph <= 0.35;
% Find indices where Ph is greater than 0.35
indices2 = Ph > 0.35;
% Apply the first equation to ‘kd’ where PhiE is less than or equal to 0.35
Kd(indices1) = 38.18.*(1-3.39.*Ph(indices1)+1.95.*Ph(indices1).^2).*1e9;
% Apply the second equation to ‘kd’ where Ph is greater than 0.35
Kd(indices2) = exp(-62.60.*PhiE(indices2)+22.58).*1e9; I have a Ph of size 139 48 101 values ranging say 0 to 0.4, now to compute Kd from Ph have two equations if Ph is equal or less than 0.35 and if Ph is greater than 0.35. I used following code to apply two equation. Have I done it correctly?
Kd = zeros(size(Ph));
% Find indices where Ph is less than or equal to 0.35
indices1 = Ph <= 0.35;
% Find indices where Ph is greater than 0.35
indices2 = Ph > 0.35;
% Apply the first equation to ‘kd’ where PhiE is less than or equal to 0.35
Kd(indices1) = 38.18.*(1-3.39.*Ph(indices1)+1.95.*Ph(indices1).^2).*1e9;
% Apply the second equation to ‘kd’ where Ph is greater than 0.35
Kd(indices2) = exp(-62.60.*PhiE(indices2)+22.58).*1e9; indexing, if statement, code MATLAB Answers — New Questions