Category: Matlab
Category Archives: Matlab
How to convert two nested for-loops to one parfor loop.
I have the following code. I want to get it in one parfor loop.
clear; clc;
% number of points:
Nx = 80;
Ny = 90;
xs = linspace(-2*pi/(3),4*pi/(3),Nx);
ys = linspace(-2*pi/(sqrt(3)),2*pi/(sqrt(3)),Ny);
% Allocate memory
ZZ = zeros(Nx,Ny,8);
XX = zeros(Nx,Ny);
YY = zeros(Nx,Ny);
for ix = 1:Nx
x = xs(ix);
for iy = 1:Ny
y = ys(iy);
FUN = fun(x,y);
% sort eigenvalues:
[~,D] = eig(FUN);
[D,I] = sort(diag(real(D)),’descend’);
evals = diag(D);
% store data:
ZZ(ix,iy,:) = diag(evals);
XX(ix,iy) = x;
YY(ix,iy) = y;
end
end
%% Plot figure
figure;
tiled = tiledlayout(2,2,"TileSpacing","tight","Padding","compact");
for q = 1:2:8
nexttile
surf(XX,YY,ZZ(:,:,q),’LineStyle’,’none’,’FaceColor’,’interp’);
view(2)
axis([-2*pi/(3) 4*pi/(3) -2*pi/(sqrt(3)) +2*pi/(sqrt(3))])
box on
grid off
axis square
colorbar
end
%%
function out = fun(x,y)
out = [ 3, 0, – 2*cos(x/4 + (3^(1/2)*y)/4) – (3*cos((3*x)/4 – (3^(1/2)*y)/4))/5, 0, – 2*cos(x/4 – (3^(1/2)*y)/4) – (3*cos((3*x)/4 + (3^(1/2)*y)/4))/5, 0, – 2*cos(x/2) – (3*cos((3^(1/2)*y)/2))/5, 0
0, -3, 0, – 2*cos(x/4 + (3^(1/2)*y)/4) – (3*cos((3*x)/4 – (3^(1/2)*y)/4))/5, 0, – 2*cos(x/4 – (3^(1/2)*y)/4) – (3*cos((3*x)/4 + (3^(1/2)*y)/4))/5, 0, – 2*cos(x/2) – (3*cos((3^(1/2)*y)/2))/5
– 2*cos(x/4 + (3^(1/2)*y)/4) – (3*cos((3*x)/4 – (3^(1/2)*y)/4))/5, 0, -1, -2*2^(1/2), – 2*cos(x/2) – (3*cos((3^(1/2)*y)/2))/5, 0, – 2*cos(x/4 – (3^(1/2)*y)/4) – (3*cos((3*x)/4 + (3^(1/2)*y)/4))/5, 0
0, – 2*cos(x/4 + (3^(1/2)*y)/4) – (3*cos((3*x)/4 – (3^(1/2)*y)/4))/5, -2*2^(1/2), 1, 0, – 2*cos(x/2) – (3*cos((3^(1/2)*y)/2))/5, 0, – 2*cos(x/4 – (3^(1/2)*y)/4) – (3*cos((3*x)/4 + (3^(1/2)*y)/4))/5
– 2*cos(x/4 – (3^(1/2)*y)/4) – (3*cos((3*x)/4 + (3^(1/2)*y)/4))/5, 0, – 2*cos(x/2) – (3*cos((3^(1/2)*y)/2))/5, 0, -1, 2^(1/2) – 6^(1/2)*1i, – 2*cos(x/4 + (3^(1/2)*y)/4) – (3*cos((3*x)/4 – (3^(1/2)*y)/4))/5, 0
0, – 2*cos(x/4 – (3^(1/2)*y)/4) – (3*cos((3*x)/4 + (3^(1/2)*y)/4))/5, 0, – 2*cos(x/2) – (3*cos((3^(1/2)*y)/2))/5, 2^(1/2) + 6^(1/2)*1i, 1, 0, – 2*cos(x/4 + (3^(1/2)*y)/4) – (3*cos((3*x)/4 – (3^(1/2)*y)/4))/5
– 2*cos(x/2) – (3*cos((3^(1/2)*y)/2))/5, 0, – 2*cos(x/4 – (3^(1/2)*y)/4) – (3*cos((3*x)/4 + (3^(1/2)*y)/4))/5, 0, – 2*cos(x/4 + (3^(1/2)*y)/4) – (3*cos((3*x)/4 – (3^(1/2)*y)/4))/5, 0, -1, 2^(1/2) + 6^(1/2)*1i
0, – 2*cos(x/2) – (3*cos((3^(1/2)*y)/2))/5, 0, – 2*cos(x/4 – (3^(1/2)*y)/4) – (3*cos((3*x)/4 + (3^(1/2)*y)/4))/5, 0, – 2*cos(x/4 + (3^(1/2)*y)/4) – (3*cos((3*x)/4 – (3^(1/2)*y)/4))/5, 2^(1/2) – 6^(1/2)*1i, 1
];
endI have the following code. I want to get it in one parfor loop.
clear; clc;
% number of points:
Nx = 80;
Ny = 90;
xs = linspace(-2*pi/(3),4*pi/(3),Nx);
ys = linspace(-2*pi/(sqrt(3)),2*pi/(sqrt(3)),Ny);
% Allocate memory
ZZ = zeros(Nx,Ny,8);
XX = zeros(Nx,Ny);
YY = zeros(Nx,Ny);
for ix = 1:Nx
x = xs(ix);
for iy = 1:Ny
y = ys(iy);
FUN = fun(x,y);
% sort eigenvalues:
[~,D] = eig(FUN);
[D,I] = sort(diag(real(D)),’descend’);
evals = diag(D);
% store data:
ZZ(ix,iy,:) = diag(evals);
XX(ix,iy) = x;
YY(ix,iy) = y;
end
end
%% Plot figure
figure;
tiled = tiledlayout(2,2,"TileSpacing","tight","Padding","compact");
for q = 1:2:8
nexttile
surf(XX,YY,ZZ(:,:,q),’LineStyle’,’none’,’FaceColor’,’interp’);
view(2)
axis([-2*pi/(3) 4*pi/(3) -2*pi/(sqrt(3)) +2*pi/(sqrt(3))])
box on
grid off
axis square
colorbar
end
%%
function out = fun(x,y)
out = [ 3, 0, – 2*cos(x/4 + (3^(1/2)*y)/4) – (3*cos((3*x)/4 – (3^(1/2)*y)/4))/5, 0, – 2*cos(x/4 – (3^(1/2)*y)/4) – (3*cos((3*x)/4 + (3^(1/2)*y)/4))/5, 0, – 2*cos(x/2) – (3*cos((3^(1/2)*y)/2))/5, 0
0, -3, 0, – 2*cos(x/4 + (3^(1/2)*y)/4) – (3*cos((3*x)/4 – (3^(1/2)*y)/4))/5, 0, – 2*cos(x/4 – (3^(1/2)*y)/4) – (3*cos((3*x)/4 + (3^(1/2)*y)/4))/5, 0, – 2*cos(x/2) – (3*cos((3^(1/2)*y)/2))/5
– 2*cos(x/4 + (3^(1/2)*y)/4) – (3*cos((3*x)/4 – (3^(1/2)*y)/4))/5, 0, -1, -2*2^(1/2), – 2*cos(x/2) – (3*cos((3^(1/2)*y)/2))/5, 0, – 2*cos(x/4 – (3^(1/2)*y)/4) – (3*cos((3*x)/4 + (3^(1/2)*y)/4))/5, 0
0, – 2*cos(x/4 + (3^(1/2)*y)/4) – (3*cos((3*x)/4 – (3^(1/2)*y)/4))/5, -2*2^(1/2), 1, 0, – 2*cos(x/2) – (3*cos((3^(1/2)*y)/2))/5, 0, – 2*cos(x/4 – (3^(1/2)*y)/4) – (3*cos((3*x)/4 + (3^(1/2)*y)/4))/5
– 2*cos(x/4 – (3^(1/2)*y)/4) – (3*cos((3*x)/4 + (3^(1/2)*y)/4))/5, 0, – 2*cos(x/2) – (3*cos((3^(1/2)*y)/2))/5, 0, -1, 2^(1/2) – 6^(1/2)*1i, – 2*cos(x/4 + (3^(1/2)*y)/4) – (3*cos((3*x)/4 – (3^(1/2)*y)/4))/5, 0
0, – 2*cos(x/4 – (3^(1/2)*y)/4) – (3*cos((3*x)/4 + (3^(1/2)*y)/4))/5, 0, – 2*cos(x/2) – (3*cos((3^(1/2)*y)/2))/5, 2^(1/2) + 6^(1/2)*1i, 1, 0, – 2*cos(x/4 + (3^(1/2)*y)/4) – (3*cos((3*x)/4 – (3^(1/2)*y)/4))/5
– 2*cos(x/2) – (3*cos((3^(1/2)*y)/2))/5, 0, – 2*cos(x/4 – (3^(1/2)*y)/4) – (3*cos((3*x)/4 + (3^(1/2)*y)/4))/5, 0, – 2*cos(x/4 + (3^(1/2)*y)/4) – (3*cos((3*x)/4 – (3^(1/2)*y)/4))/5, 0, -1, 2^(1/2) + 6^(1/2)*1i
0, – 2*cos(x/2) – (3*cos((3^(1/2)*y)/2))/5, 0, – 2*cos(x/4 – (3^(1/2)*y)/4) – (3*cos((3*x)/4 + (3^(1/2)*y)/4))/5, 0, – 2*cos(x/4 + (3^(1/2)*y)/4) – (3*cos((3*x)/4 – (3^(1/2)*y)/4))/5, 2^(1/2) – 6^(1/2)*1i, 1
];
end I have the following code. I want to get it in one parfor loop.
clear; clc;
% number of points:
Nx = 80;
Ny = 90;
xs = linspace(-2*pi/(3),4*pi/(3),Nx);
ys = linspace(-2*pi/(sqrt(3)),2*pi/(sqrt(3)),Ny);
% Allocate memory
ZZ = zeros(Nx,Ny,8);
XX = zeros(Nx,Ny);
YY = zeros(Nx,Ny);
for ix = 1:Nx
x = xs(ix);
for iy = 1:Ny
y = ys(iy);
FUN = fun(x,y);
% sort eigenvalues:
[~,D] = eig(FUN);
[D,I] = sort(diag(real(D)),’descend’);
evals = diag(D);
% store data:
ZZ(ix,iy,:) = diag(evals);
XX(ix,iy) = x;
YY(ix,iy) = y;
end
end
%% Plot figure
figure;
tiled = tiledlayout(2,2,"TileSpacing","tight","Padding","compact");
for q = 1:2:8
nexttile
surf(XX,YY,ZZ(:,:,q),’LineStyle’,’none’,’FaceColor’,’interp’);
view(2)
axis([-2*pi/(3) 4*pi/(3) -2*pi/(sqrt(3)) +2*pi/(sqrt(3))])
box on
grid off
axis square
colorbar
end
%%
function out = fun(x,y)
out = [ 3, 0, – 2*cos(x/4 + (3^(1/2)*y)/4) – (3*cos((3*x)/4 – (3^(1/2)*y)/4))/5, 0, – 2*cos(x/4 – (3^(1/2)*y)/4) – (3*cos((3*x)/4 + (3^(1/2)*y)/4))/5, 0, – 2*cos(x/2) – (3*cos((3^(1/2)*y)/2))/5, 0
0, -3, 0, – 2*cos(x/4 + (3^(1/2)*y)/4) – (3*cos((3*x)/4 – (3^(1/2)*y)/4))/5, 0, – 2*cos(x/4 – (3^(1/2)*y)/4) – (3*cos((3*x)/4 + (3^(1/2)*y)/4))/5, 0, – 2*cos(x/2) – (3*cos((3^(1/2)*y)/2))/5
– 2*cos(x/4 + (3^(1/2)*y)/4) – (3*cos((3*x)/4 – (3^(1/2)*y)/4))/5, 0, -1, -2*2^(1/2), – 2*cos(x/2) – (3*cos((3^(1/2)*y)/2))/5, 0, – 2*cos(x/4 – (3^(1/2)*y)/4) – (3*cos((3*x)/4 + (3^(1/2)*y)/4))/5, 0
0, – 2*cos(x/4 + (3^(1/2)*y)/4) – (3*cos((3*x)/4 – (3^(1/2)*y)/4))/5, -2*2^(1/2), 1, 0, – 2*cos(x/2) – (3*cos((3^(1/2)*y)/2))/5, 0, – 2*cos(x/4 – (3^(1/2)*y)/4) – (3*cos((3*x)/4 + (3^(1/2)*y)/4))/5
– 2*cos(x/4 – (3^(1/2)*y)/4) – (3*cos((3*x)/4 + (3^(1/2)*y)/4))/5, 0, – 2*cos(x/2) – (3*cos((3^(1/2)*y)/2))/5, 0, -1, 2^(1/2) – 6^(1/2)*1i, – 2*cos(x/4 + (3^(1/2)*y)/4) – (3*cos((3*x)/4 – (3^(1/2)*y)/4))/5, 0
0, – 2*cos(x/4 – (3^(1/2)*y)/4) – (3*cos((3*x)/4 + (3^(1/2)*y)/4))/5, 0, – 2*cos(x/2) – (3*cos((3^(1/2)*y)/2))/5, 2^(1/2) + 6^(1/2)*1i, 1, 0, – 2*cos(x/4 + (3^(1/2)*y)/4) – (3*cos((3*x)/4 – (3^(1/2)*y)/4))/5
– 2*cos(x/2) – (3*cos((3^(1/2)*y)/2))/5, 0, – 2*cos(x/4 – (3^(1/2)*y)/4) – (3*cos((3*x)/4 + (3^(1/2)*y)/4))/5, 0, – 2*cos(x/4 + (3^(1/2)*y)/4) – (3*cos((3*x)/4 – (3^(1/2)*y)/4))/5, 0, -1, 2^(1/2) + 6^(1/2)*1i
0, – 2*cos(x/2) – (3*cos((3^(1/2)*y)/2))/5, 0, – 2*cos(x/4 – (3^(1/2)*y)/4) – (3*cos((3*x)/4 + (3^(1/2)*y)/4))/5, 0, – 2*cos(x/4 + (3^(1/2)*y)/4) – (3*cos((3*x)/4 – (3^(1/2)*y)/4))/5, 2^(1/2) – 6^(1/2)*1i, 1
];
end parfor MATLAB Answers — New Questions
Write a code in Matlab to generate a realistic synthetic ECG signal (which includes the noise aspect) , demonstrate Visualization of the signal in both the time and frequency
It must include the noise aspect, can l have a sample of the codeIt must include the noise aspect, can l have a sample of the code It must include the noise aspect, can l have a sample of the code kz MATLAB Answers — New Questions
JSON Syntax Error in Simulink Arduino Setup with Flatpak
On NixOS (glnxa64), when I have Flatpak enabled, I will receive an error from the following:
Install Simulink Support Package for Arduino Hardware add-on
Start Hardware Setup wizard in said add-on
Get to the stage where the wizard prompts me to Connect an Arduino Board and is detecting boards
Receive error "JSON syntax error at line 1 (character 1), expected ‘false’ but found ‘flatpak’"
There seem to be no log created for this error (or for the Hardware Setup wizard in general), so it’s been difficult for me to debug the exact reason that this is occuring.
The only way I’ve found to work around this error is to remove Flatpak from my system during Hardware Setup, and re-enable it later. Though there is no guarantee that Arduino support in Simulink will continue to work after I’ve re-enabled Flatpak (I’ve not tested), so I thought it’s important nevertheless for me to raise this issue.On NixOS (glnxa64), when I have Flatpak enabled, I will receive an error from the following:
Install Simulink Support Package for Arduino Hardware add-on
Start Hardware Setup wizard in said add-on
Get to the stage where the wizard prompts me to Connect an Arduino Board and is detecting boards
Receive error "JSON syntax error at line 1 (character 1), expected ‘false’ but found ‘flatpak’"
There seem to be no log created for this error (or for the Hardware Setup wizard in general), so it’s been difficult for me to debug the exact reason that this is occuring.
The only way I’ve found to work around this error is to remove Flatpak from my system during Hardware Setup, and re-enable it later. Though there is no guarantee that Arduino support in Simulink will continue to work after I’ve re-enabled Flatpak (I’ve not tested), so I thought it’s important nevertheless for me to raise this issue. On NixOS (glnxa64), when I have Flatpak enabled, I will receive an error from the following:
Install Simulink Support Package for Arduino Hardware add-on
Start Hardware Setup wizard in said add-on
Get to the stage where the wizard prompts me to Connect an Arduino Board and is detecting boards
Receive error "JSON syntax error at line 1 (character 1), expected ‘false’ but found ‘flatpak’"
There seem to be no log created for this error (or for the Hardware Setup wizard in general), so it’s been difficult for me to debug the exact reason that this is occuring.
The only way I’ve found to work around this error is to remove Flatpak from my system during Hardware Setup, and re-enable it later. Though there is no guarantee that Arduino support in Simulink will continue to work after I’ve re-enabled Flatpak (I’ve not tested), so I thought it’s important nevertheless for me to raise this issue. arduino, simulink, support package, hardware setup MATLAB Answers — New Questions
Any alternative for diode to make the model linear for real-time simulation?
Hello,
I am optimizing matlab/simulink model to run in real-time.
To make the model less computationally expensive, I’m optimizing the model like replacing the switches with variable resistance.
I was wondering if anybody has any suggestions for me to replace the diodes with a linear component.
Please let me know.
Thanks in advanceHello,
I am optimizing matlab/simulink model to run in real-time.
To make the model less computationally expensive, I’m optimizing the model like replacing the switches with variable resistance.
I was wondering if anybody has any suggestions for me to replace the diodes with a linear component.
Please let me know.
Thanks in advance Hello,
I am optimizing matlab/simulink model to run in real-time.
To make the model less computationally expensive, I’m optimizing the model like replacing the switches with variable resistance.
I was wondering if anybody has any suggestions for me to replace the diodes with a linear component.
Please let me know.
Thanks in advance real-time simulation, optimization, diode MATLAB Answers — New Questions
Custom Sugeno Fuzzy Model
How can I use custom output fuctions instead of ‘linear’ and ‘contant’ in a Sugeno Model?How can I use custom output fuctions instead of ‘linear’ and ‘contant’ in a Sugeno Model? How can I use custom output fuctions instead of ‘linear’ and ‘contant’ in a Sugeno Model? fuzzy MATLAB Answers — New Questions
Deployed Matlab function with strage errors: functions not found
Hi there,
I have a new company PC and had to install MATLAB on this PC. I’m currently facing some strange behaviour when deploying my functions and tools written with MATLAB.
I’m using ether MATLAB 2015aSP1 or 2015b as those two versions were the last providing 32bit support. The programs had to run also on older machine PCs therefore this restriction.
However when deploying even a simple function or small tool I always get some strange errors like the self written functions are not found (those functions are in a folder known to MATLAB). When running the functions or programs by MATLAB it works fine. The deployed version does not run.
I’ve installed the latest update for both MATLAB versions.
Im Using Windows 10 Enterprise (Version 10.019045). The MATLAB version is 8.5.1.959712 (R2015a) Service Pack 1 Update 3.
The first error is in which I run with startup.m
savepath(‘pathdef.m’);
The error says "Error using savepath. Too many input arguments"
Canceling out this line I get errors on other lines. It seems that the depolyed version does not find the folder with all the user functions.
The make it more complicated: on my old company PC it worked fine.
Kind regards,
ChristianHi there,
I have a new company PC and had to install MATLAB on this PC. I’m currently facing some strange behaviour when deploying my functions and tools written with MATLAB.
I’m using ether MATLAB 2015aSP1 or 2015b as those two versions were the last providing 32bit support. The programs had to run also on older machine PCs therefore this restriction.
However when deploying even a simple function or small tool I always get some strange errors like the self written functions are not found (those functions are in a folder known to MATLAB). When running the functions or programs by MATLAB it works fine. The deployed version does not run.
I’ve installed the latest update for both MATLAB versions.
Im Using Windows 10 Enterprise (Version 10.019045). The MATLAB version is 8.5.1.959712 (R2015a) Service Pack 1 Update 3.
The first error is in which I run with startup.m
savepath(‘pathdef.m’);
The error says "Error using savepath. Too many input arguments"
Canceling out this line I get errors on other lines. It seems that the depolyed version does not find the folder with all the user functions.
The make it more complicated: on my old company PC it worked fine.
Kind regards,
Christian Hi there,
I have a new company PC and had to install MATLAB on this PC. I’m currently facing some strange behaviour when deploying my functions and tools written with MATLAB.
I’m using ether MATLAB 2015aSP1 or 2015b as those two versions were the last providing 32bit support. The programs had to run also on older machine PCs therefore this restriction.
However when deploying even a simple function or small tool I always get some strange errors like the self written functions are not found (those functions are in a folder known to MATLAB). When running the functions or programs by MATLAB it works fine. The deployed version does not run.
I’ve installed the latest update for both MATLAB versions.
Im Using Windows 10 Enterprise (Version 10.019045). The MATLAB version is 8.5.1.959712 (R2015a) Service Pack 1 Update 3.
The first error is in which I run with startup.m
savepath(‘pathdef.m’);
The error says "Error using savepath. Too many input arguments"
Canceling out this line I get errors on other lines. It seems that the depolyed version does not find the folder with all the user functions.
The make it more complicated: on my old company PC it worked fine.
Kind regards,
Christian mcc, deploy, pathdef, savepath, startup, compiler MATLAB Answers — New Questions
Cannot get fmincon to work
Hello, I am trying to find the max value of a function of 5 variables with all of them constrained between 2 values. However, I do not have experience using this function and I can’t find any good information to use it properly in this scenario either in the MatLab help page, nor in any online tutorial.
As you can see in the code, the function I want to optimize is P, and the variables are V, na, nd, wn and wp.
Apologies if the code is too hard to understand.
clear
close
clc
format long
%% Variables
dp = 11.6;
taup = 3710e-6;
dn = 2;
taun = 371e-6;
Jl = 50e-3;
ni = 9.696e9;
q = 1.602176634e-19;
k = 1.3880649e-23;
T = 300;
ep = 1.035918e-12;
vint = @(na,nd) k.*T/q.*log((na.*nd)./ni^2);
xn = @(na,nd) sqrt((2.*ep.*vint(na,nd).*na)./(q.*nd.*(na+nd)));
xp = @(na,nd) sqrt((2.*ep.*vint(na,nd).*nd)./(q.*na.*(na+nd)));
dnxp = @(V,na) ni^2./na.*(exp(q*V./(k*T))-1);
dpxn = @(V,nd) ni^2./nd.*(exp(q*V./(k*T))-1);
%% Functions
C1 = @(V,na,nd,wn) dnxp(V,na)./(-exp((-2.*wn-xp(na,nd))./sqrt(dn*taun))+exp(xp(na,nd)./sqrt(dn*taun)));
C3 = @(V,na,nd,wp) dpxn(V,nd)./(exp(-xn(na,nd)./sqrt(taup*dp))-exp((2.*wp+xn(na,nd))./sqrt(taup*dp)));
Jn = @(x,V,na,nd,wn) q*sqrt(dn/taun).*C1(V,na,nd,wn).*(exp((-2.*wn-x)./sqrt(dn*taun))+exp(x./sqrt(dn*taun)));
Jp = @(x,V,na,nd,wp) -q*sqrt(dp/taup).*C3(V,na,nd,wp).*(exp(x./sqrt(taup*dp))+exp((2.*wp-x)./sqrt(taup*dp)));
Jd = @(V,na,nd,wn,wp) Jp(-xn(na,nd),V,na,nd,wn)+Jn(xp(na,nd),V,na,nd,wp);
J = @(V,na,nd,wn,wp) Jl-Jd(V,na,nd,wn,wp);
P = @(V,na,nd,wn,wp) J(V,na,nd,wn,wp).*V;
%% Boundaries
wn = [0.05e-4, 0.75e-4];
wp = [100e-4, 200e-4];
nd = [1e18, 5e20];
na = [1e14, 1e16];
V = [0, 0.52];
P0 = [(V(1)+V(2))/2; (wn(1)+wn(2))/2; (wp(1)+wp(2))/2; (nd(1)+nd(2))/2; (na(1)+na(2))/2];
%% Optimization
mx = fmincon(-P(V,wn,wp,nd,na),P0,[],[],[],[],[min(V),wn(1),wp(1),nd(1),na(1)],[max(V),wn(2),wp(2),nd(2),na(2)]);Hello, I am trying to find the max value of a function of 5 variables with all of them constrained between 2 values. However, I do not have experience using this function and I can’t find any good information to use it properly in this scenario either in the MatLab help page, nor in any online tutorial.
As you can see in the code, the function I want to optimize is P, and the variables are V, na, nd, wn and wp.
Apologies if the code is too hard to understand.
clear
close
clc
format long
%% Variables
dp = 11.6;
taup = 3710e-6;
dn = 2;
taun = 371e-6;
Jl = 50e-3;
ni = 9.696e9;
q = 1.602176634e-19;
k = 1.3880649e-23;
T = 300;
ep = 1.035918e-12;
vint = @(na,nd) k.*T/q.*log((na.*nd)./ni^2);
xn = @(na,nd) sqrt((2.*ep.*vint(na,nd).*na)./(q.*nd.*(na+nd)));
xp = @(na,nd) sqrt((2.*ep.*vint(na,nd).*nd)./(q.*na.*(na+nd)));
dnxp = @(V,na) ni^2./na.*(exp(q*V./(k*T))-1);
dpxn = @(V,nd) ni^2./nd.*(exp(q*V./(k*T))-1);
%% Functions
C1 = @(V,na,nd,wn) dnxp(V,na)./(-exp((-2.*wn-xp(na,nd))./sqrt(dn*taun))+exp(xp(na,nd)./sqrt(dn*taun)));
C3 = @(V,na,nd,wp) dpxn(V,nd)./(exp(-xn(na,nd)./sqrt(taup*dp))-exp((2.*wp+xn(na,nd))./sqrt(taup*dp)));
Jn = @(x,V,na,nd,wn) q*sqrt(dn/taun).*C1(V,na,nd,wn).*(exp((-2.*wn-x)./sqrt(dn*taun))+exp(x./sqrt(dn*taun)));
Jp = @(x,V,na,nd,wp) -q*sqrt(dp/taup).*C3(V,na,nd,wp).*(exp(x./sqrt(taup*dp))+exp((2.*wp-x)./sqrt(taup*dp)));
Jd = @(V,na,nd,wn,wp) Jp(-xn(na,nd),V,na,nd,wn)+Jn(xp(na,nd),V,na,nd,wp);
J = @(V,na,nd,wn,wp) Jl-Jd(V,na,nd,wn,wp);
P = @(V,na,nd,wn,wp) J(V,na,nd,wn,wp).*V;
%% Boundaries
wn = [0.05e-4, 0.75e-4];
wp = [100e-4, 200e-4];
nd = [1e18, 5e20];
na = [1e14, 1e16];
V = [0, 0.52];
P0 = [(V(1)+V(2))/2; (wn(1)+wn(2))/2; (wp(1)+wp(2))/2; (nd(1)+nd(2))/2; (na(1)+na(2))/2];
%% Optimization
mx = fmincon(-P(V,wn,wp,nd,na),P0,[],[],[],[],[min(V),wn(1),wp(1),nd(1),na(1)],[max(V),wn(2),wp(2),nd(2),na(2)]); Hello, I am trying to find the max value of a function of 5 variables with all of them constrained between 2 values. However, I do not have experience using this function and I can’t find any good information to use it properly in this scenario either in the MatLab help page, nor in any online tutorial.
As you can see in the code, the function I want to optimize is P, and the variables are V, na, nd, wn and wp.
Apologies if the code is too hard to understand.
clear
close
clc
format long
%% Variables
dp = 11.6;
taup = 3710e-6;
dn = 2;
taun = 371e-6;
Jl = 50e-3;
ni = 9.696e9;
q = 1.602176634e-19;
k = 1.3880649e-23;
T = 300;
ep = 1.035918e-12;
vint = @(na,nd) k.*T/q.*log((na.*nd)./ni^2);
xn = @(na,nd) sqrt((2.*ep.*vint(na,nd).*na)./(q.*nd.*(na+nd)));
xp = @(na,nd) sqrt((2.*ep.*vint(na,nd).*nd)./(q.*na.*(na+nd)));
dnxp = @(V,na) ni^2./na.*(exp(q*V./(k*T))-1);
dpxn = @(V,nd) ni^2./nd.*(exp(q*V./(k*T))-1);
%% Functions
C1 = @(V,na,nd,wn) dnxp(V,na)./(-exp((-2.*wn-xp(na,nd))./sqrt(dn*taun))+exp(xp(na,nd)./sqrt(dn*taun)));
C3 = @(V,na,nd,wp) dpxn(V,nd)./(exp(-xn(na,nd)./sqrt(taup*dp))-exp((2.*wp+xn(na,nd))./sqrt(taup*dp)));
Jn = @(x,V,na,nd,wn) q*sqrt(dn/taun).*C1(V,na,nd,wn).*(exp((-2.*wn-x)./sqrt(dn*taun))+exp(x./sqrt(dn*taun)));
Jp = @(x,V,na,nd,wp) -q*sqrt(dp/taup).*C3(V,na,nd,wp).*(exp(x./sqrt(taup*dp))+exp((2.*wp-x)./sqrt(taup*dp)));
Jd = @(V,na,nd,wn,wp) Jp(-xn(na,nd),V,na,nd,wn)+Jn(xp(na,nd),V,na,nd,wp);
J = @(V,na,nd,wn,wp) Jl-Jd(V,na,nd,wn,wp);
P = @(V,na,nd,wn,wp) J(V,na,nd,wn,wp).*V;
%% Boundaries
wn = [0.05e-4, 0.75e-4];
wp = [100e-4, 200e-4];
nd = [1e18, 5e20];
na = [1e14, 1e16];
V = [0, 0.52];
P0 = [(V(1)+V(2))/2; (wn(1)+wn(2))/2; (wp(1)+wp(2))/2; (nd(1)+nd(2))/2; (na(1)+na(2))/2];
%% Optimization
mx = fmincon(-P(V,wn,wp,nd,na),P0,[],[],[],[],[min(V),wn(1),wp(1),nd(1),na(1)],[max(V),wn(2),wp(2),nd(2),na(2)]); optimization, fmincon MATLAB Answers — New Questions
Error in Integrator block
Error:An error occurred during simulation and the simulation was terminated
Caused by:
Derivative of state ‘1’ in block ‘HW02_2024_SimulinkModel/VD model/velocity’ at time 64.05749999999999 is not finite. The simulation will be stopped. There may be a singularity in the solution. If not, try reducing the step size (either by reducing the fixed step size or by tightening the error tolerances)
at my integrator block.Error:An error occurred during simulation and the simulation was terminated
Caused by:
Derivative of state ‘1’ in block ‘HW02_2024_SimulinkModel/VD model/velocity’ at time 64.05749999999999 is not finite. The simulation will be stopped. There may be a singularity in the solution. If not, try reducing the step size (either by reducing the fixed step size or by tightening the error tolerances)
at my integrator block. Error:An error occurred during simulation and the simulation was terminated
Caused by:
Derivative of state ‘1’ in block ‘HW02_2024_SimulinkModel/VD model/velocity’ at time 64.05749999999999 is not finite. The simulation will be stopped. There may be a singularity in the solution. If not, try reducing the step size (either by reducing the fixed step size or by tightening the error tolerances)
at my integrator block. simulink, matlab compiler MATLAB Answers — New Questions
Too Much Variables in Fuzzy Logic and ANFIS
Hi i have 13 variables and some of them has 5 membership. I can’t define all rules (1 billion rules). How can i define that rules?
And my data has 16000 rows how can i train fis without memory problem?
P.S.I can only use some of GUI’s so please keep it simple.
ThxHi i have 13 variables and some of them has 5 membership. I can’t define all rules (1 billion rules). How can i define that rules?
And my data has 16000 rows how can i train fis without memory problem?
P.S.I can only use some of GUI’s so please keep it simple.
Thx Hi i have 13 variables and some of them has 5 membership. I can’t define all rules (1 billion rules). How can i define that rules?
And my data has 16000 rows how can i train fis without memory problem?
P.S.I can only use some of GUI’s so please keep it simple.
Thx fuzzy, variables, membership, fuzzy rules, anfis MATLAB Answers — New Questions
Calculation not having the desired array size
I am trying to calcumate the Acceleration using a rotational matrix, during the loop the left side is a 3×14074 (good), but the right is (3×3) which I don’t want. How can I fix this so the right side has a matching size? Thanks
% Acc and AngV are sensor data sets and have sizes of 14074×3
% qF is 3x3x14074
g = [0,0,9.81];
xn = zeros(3, length(Acc));
fuse = imufilter(‘SampleRate’,100,’DecimationFactor’,1);
q = fuse(Acc, AngV);
qF = quat2rotm(q);
for i=1:length(AngV)
xn(1:3,:) = qF(:,:,i) .* Acc(i,:)’ – g; % This is where the error arises
end
vi = xn .* dt;
ri = .5 * xn .* (dt^2);
I’ve tried every combination of transforms and reordering that I could think of, everything gives 3×3 or 3×1. Somehow I used to have this work for me, then I woke up and bam, its broken.
Error:
Unable to perform assignment because the size of the left side is 3-by-14074 and the size of the right side is 3-by-3.
Error in RunKalman (line 27)
xn(1:3, 🙂 = qF(:, :, i)’ .* Acc(i, :)’ – g;I am trying to calcumate the Acceleration using a rotational matrix, during the loop the left side is a 3×14074 (good), but the right is (3×3) which I don’t want. How can I fix this so the right side has a matching size? Thanks
% Acc and AngV are sensor data sets and have sizes of 14074×3
% qF is 3x3x14074
g = [0,0,9.81];
xn = zeros(3, length(Acc));
fuse = imufilter(‘SampleRate’,100,’DecimationFactor’,1);
q = fuse(Acc, AngV);
qF = quat2rotm(q);
for i=1:length(AngV)
xn(1:3,:) = qF(:,:,i) .* Acc(i,:)’ – g; % This is where the error arises
end
vi = xn .* dt;
ri = .5 * xn .* (dt^2);
I’ve tried every combination of transforms and reordering that I could think of, everything gives 3×3 or 3×1. Somehow I used to have this work for me, then I woke up and bam, its broken.
Error:
Unable to perform assignment because the size of the left side is 3-by-14074 and the size of the right side is 3-by-3.
Error in RunKalman (line 27)
xn(1:3, 🙂 = qF(:, :, i)’ .* Acc(i, :)’ – g; I am trying to calcumate the Acceleration using a rotational matrix, during the loop the left side is a 3×14074 (good), but the right is (3×3) which I don’t want. How can I fix this so the right side has a matching size? Thanks
% Acc and AngV are sensor data sets and have sizes of 14074×3
% qF is 3x3x14074
g = [0,0,9.81];
xn = zeros(3, length(Acc));
fuse = imufilter(‘SampleRate’,100,’DecimationFactor’,1);
q = fuse(Acc, AngV);
qF = quat2rotm(q);
for i=1:length(AngV)
xn(1:3,:) = qF(:,:,i) .* Acc(i,:)’ – g; % This is where the error arises
end
vi = xn .* dt;
ri = .5 * xn .* (dt^2);
I’ve tried every combination of transforms and reordering that I could think of, everything gives 3×3 or 3×1. Somehow I used to have this work for me, then I woke up and bam, its broken.
Error:
Unable to perform assignment because the size of the left side is 3-by-14074 and the size of the right side is 3-by-3.
Error in RunKalman (line 27)
xn(1:3, 🙂 = qF(:, :, i)’ .* Acc(i, :)’ – g; matrix manipulation, matrix array MATLAB Answers — New Questions
how to plot 4D figure to describe changing in ocean ?
Hi,
I have the oceanographic data ( Longitude , Latitude , Density ,Depth)
I want to plot the change of density with depth such as the picture
I try to plot with surf but give me the surface changing
Also I try slice but did’t work because my data is 2D
Longitude =300*400,
Latitude =300*400 ,
Density =300*400,
Depth =300*400Hi,
I have the oceanographic data ( Longitude , Latitude , Density ,Depth)
I want to plot the change of density with depth such as the picture
I try to plot with surf but give me the surface changing
Also I try slice but did’t work because my data is 2D
Longitude =300*400,
Latitude =300*400 ,
Density =300*400,
Depth =300*400 Hi,
I have the oceanographic data ( Longitude , Latitude , Density ,Depth)
I want to plot the change of density with depth such as the picture
I try to plot with surf but give me the surface changing
Also I try slice but did’t work because my data is 2D
Longitude =300*400,
Latitude =300*400 ,
Density =300*400,
Depth =300*400 surf, 3dplot, slice MATLAB Answers — New Questions
x(n)=u(n-2)-u(n-5)
x(n)=u(n-2)-u(n-5)x(n)=u(n-2)-u(n-5) x(n)=u(n-2)-u(n-5) x(n)=u(n-2)-u(n-5) MATLAB Answers — New Questions
You need to perform the analysis of braking by following the same methodology that was used to analyze traction/acceleration in the session.
The following are the deliverables (using Matlab):
Develop the Kf_Braking script file (similar to Kf_Traction file that has been shared) to plot the deceleration vs. Kf curve. Here, the Kf indicates the front braking torque distribution factor. You need to submit this plot indicating the:
Kf_opt value (ensure it matches with the value from the analytical expression);
no slip, the rear wheel skid & front wheel skid regions.
(You are free to choose & coefficient of friction.)
Develop the Vehicle_Braking script file (similar to Vehicle_Traction file that has been shared).
Starting from rest, accelerate the vehicle to reach a speed of 30m/s by applying an appropriate T_in (the wheels shouldn’t slip)
Apply brakes after 5 seconds (the vehicle moves with a constant speed in this time span). Consider any T_app for any Kf < Kf_opt, Kf = Kf_opt & Kf > Kf_opt
T_app < T_opt
T_app = T_opt
T_opt < T_app < T_sat
T_app = T_sat
T_app > T_sat
Find out the values of the normal reactions, the braking forces, the deceleration & the stopping distance both analytically & virtually (simulation). Prepare a table & share it along with the Matlab screenshot of the display chart highlighting these values.The following are the deliverables (using Matlab):
Develop the Kf_Braking script file (similar to Kf_Traction file that has been shared) to plot the deceleration vs. Kf curve. Here, the Kf indicates the front braking torque distribution factor. You need to submit this plot indicating the:
Kf_opt value (ensure it matches with the value from the analytical expression);
no slip, the rear wheel skid & front wheel skid regions.
(You are free to choose & coefficient of friction.)
Develop the Vehicle_Braking script file (similar to Vehicle_Traction file that has been shared).
Starting from rest, accelerate the vehicle to reach a speed of 30m/s by applying an appropriate T_in (the wheels shouldn’t slip)
Apply brakes after 5 seconds (the vehicle moves with a constant speed in this time span). Consider any T_app for any Kf < Kf_opt, Kf = Kf_opt & Kf > Kf_opt
T_app < T_opt
T_app = T_opt
T_opt < T_app < T_sat
T_app = T_sat
T_app > T_sat
Find out the values of the normal reactions, the braking forces, the deceleration & the stopping distance both analytically & virtually (simulation). Prepare a table & share it along with the Matlab screenshot of the display chart highlighting these values. The following are the deliverables (using Matlab):
Develop the Kf_Braking script file (similar to Kf_Traction file that has been shared) to plot the deceleration vs. Kf curve. Here, the Kf indicates the front braking torque distribution factor. You need to submit this plot indicating the:
Kf_opt value (ensure it matches with the value from the analytical expression);
no slip, the rear wheel skid & front wheel skid regions.
(You are free to choose & coefficient of friction.)
Develop the Vehicle_Braking script file (similar to Vehicle_Traction file that has been shared).
Starting from rest, accelerate the vehicle to reach a speed of 30m/s by applying an appropriate T_in (the wheels shouldn’t slip)
Apply brakes after 5 seconds (the vehicle moves with a constant speed in this time span). Consider any T_app for any Kf < Kf_opt, Kf = Kf_opt & Kf > Kf_opt
T_app < T_opt
T_app = T_opt
T_opt < T_app < T_sat
T_app = T_sat
T_app > T_sat
Find out the values of the normal reactions, the braking forces, the deceleration & the stopping distance both analytically & virtually (simulation). Prepare a table & share it along with the Matlab screenshot of the display chart highlighting these values. matlab MATLAB Answers — New Questions
Stellar motion part 2, task 5
I’m doing the 5th task of Stellar motion part 2 and I keep getting it marked as wrong.
The solution to the task is
legend(starnames)
and even though I’ve tried a million ways of reentering it (restarting the task, writing it manually, copying and pasting the solution code), I keep getting a message telling me that "’legend’ appears to be both a function and a variable". I can’t keep going with the intro if this isn’t accepted. Help :(I’m doing the 5th task of Stellar motion part 2 and I keep getting it marked as wrong.
The solution to the task is
legend(starnames)
and even though I’ve tried a million ways of reentering it (restarting the task, writing it manually, copying and pasting the solution code), I keep getting a message telling me that "’legend’ appears to be both a function and a variable". I can’t keep going with the intro if this isn’t accepted. Help 🙁 I’m doing the 5th task of Stellar motion part 2 and I keep getting it marked as wrong.
The solution to the task is
legend(starnames)
and even though I’ve tried a million ways of reentering it (restarting the task, writing it manually, copying and pasting the solution code), I keep getting a message telling me that "’legend’ appears to be both a function and a variable". I can’t keep going with the intro if this isn’t accepted. Help 🙁 error, legend, array MATLAB Answers — New Questions
why is the data different after denormalization
I used ARESLAB on matlab and obtained an R2 for train and test greater than 99.0% with a MAE and MSE less than 10-3. Initially, I had normalized the database with Zcore, so after getting these results, I wanted to plot them so I wanted to denormalize the data. But the results obtained sometimes gave predictive variables different from those measured, even though the R2 was good and the errors were relatively good.I used ARESLAB on matlab and obtained an R2 for train and test greater than 99.0% with a MAE and MSE less than 10-3. Initially, I had normalized the database with Zcore, so after getting these results, I wanted to plot them so I wanted to denormalize the data. But the results obtained sometimes gave predictive variables different from those measured, even though the R2 was good and the errors were relatively good. I used ARESLAB on matlab and obtained an R2 for train and test greater than 99.0% with a MAE and MSE less than 10-3. Initially, I had normalized the database with Zcore, so after getting these results, I wanted to plot them so I wanted to denormalize the data. But the results obtained sometimes gave predictive variables different from those measured, even though the R2 was good and the errors were relatively good. ai mars areslab MATLAB Answers — New Questions
How should I plot a square wave signal by utilising fourier series?
How should I code this square wave by utlising fourier series signal? I do not have any idea how to do so.
The amplitude and period of the graph is both 1.How should I code this square wave by utlising fourier series signal? I do not have any idea how to do so.
The amplitude and period of the graph is both 1. How should I code this square wave by utlising fourier series signal? I do not have any idea how to do so.
The amplitude and period of the graph is both 1. student, homework, square wave, fourier series MATLAB Answers — New Questions
I can’t see the settling time in the bilevel measurements. How do I fix this?
Post Content Post Content bilevel measurements, settling time MATLAB Answers — New Questions
Can’t bring up matlab
Can’t bring up matlab. Every time I open the app I have to activate it. Now that it is activated, nothing more happens.Can’t bring up matlab. Every time I open the app I have to activate it. Now that it is activated, nothing more happens. Can’t bring up matlab. Every time I open the app I have to activate it. Now that it is activated, nothing more happens. cant open MATLAB Answers — New Questions
Improving personal Fourier Interpolation Model
Hi i’m trying to interpolate a cyclical set of data through a fourier transform model. I can’t just use intrpft because i need the coefficients (amplitude, frequency and phase) to project the wave equation into future. I attach the program.
Basically i use the FFT to find n cycles amplitude and frequency. Then i sync each equation with the data. At the end i superimpose all the n cycles to find the wave equation.
I reached an R2 of 0.71.
The model interpolates very well frequencies and phases but there is some problems with amplitudes i guess.
I wonder if somebody can improve precision further.
ThanksHi i’m trying to interpolate a cyclical set of data through a fourier transform model. I can’t just use intrpft because i need the coefficients (amplitude, frequency and phase) to project the wave equation into future. I attach the program.
Basically i use the FFT to find n cycles amplitude and frequency. Then i sync each equation with the data. At the end i superimpose all the n cycles to find the wave equation.
I reached an R2 of 0.71.
The model interpolates very well frequencies and phases but there is some problems with amplitudes i guess.
I wonder if somebody can improve precision further.
Thanks Hi i’m trying to interpolate a cyclical set of data through a fourier transform model. I can’t just use intrpft because i need the coefficients (amplitude, frequency and phase) to project the wave equation into future. I attach the program.
Basically i use the FFT to find n cycles amplitude and frequency. Then i sync each equation with the data. At the end i superimpose all the n cycles to find the wave equation.
I reached an R2 of 0.71.
The model interpolates very well frequencies and phases but there is some problems with amplitudes i guess.
I wonder if somebody can improve precision further.
Thanks fourier, interpolation MATLAB Answers — New Questions
Ordering the elements of cells uniquely
I have n cells. Each cell contains a couple of numbers. I need to uniquely order the numbers and save it in a matrix. How is that possible?I have n cells. Each cell contains a couple of numbers. I need to uniquely order the numbers and save it in a matrix. How is that possible? I have n cells. Each cell contains a couple of numbers. I need to uniquely order the numbers and save it in a matrix. How is that possible? cell arrays MATLAB Answers — New Questions