Category: Matlab
Category Archives: Matlab
Can I use a student license if I am not currently enrolled in courses?
I have been accepted to a university for a master’s degree, and I am learning MATLAB and reviewing other courses before I start it, which will be in Fall 2025. My university has a campus-wide license. Since I was accepted into the university, I have a student email address, even though I am not currently enrolled in any classes. Would I be able to use my university’s campus access, or should I consider purchasing a home license?I have been accepted to a university for a master’s degree, and I am learning MATLAB and reviewing other courses before I start it, which will be in Fall 2025. My university has a campus-wide license. Since I was accepted into the university, I have a student email address, even though I am not currently enrolled in any classes. Would I be able to use my university’s campus access, or should I consider purchasing a home license? I have been accepted to a university for a master’s degree, and I am learning MATLAB and reviewing other courses before I start it, which will be in Fall 2025. My university has a campus-wide license. Since I was accepted into the university, I have a student email address, even though I am not currently enrolled in any classes. Would I be able to use my university’s campus access, or should I consider purchasing a home license? student license MATLAB Answers — New Questions
Spatial discretization has failed. Discretization supports only parabolic and elliptic equations, with flux term involving spatial derivative.
I tried to establish a dynamic model of the reactor using PDEs, but I couldn’t solve it. I don’t know why. The code is as follows:
In this problem, my boundary condition only has the left boundary, which is the u value and dudx when z=0. How should I write it?
function reactor_dynamic_model(obj)
global R miu comp_set A_for A_rev Ea_for Ea_rev Da lambda
Da = 1.8e-4;
lambda = 5e-4;
F_molar = obj.F_molar;
A = obj.cat_vol / obj.z0;
z0 = obj.z0;
y0 = obj.y0;
epsilon = obj.epsilon;
P = obj.P;
T0 = obj.T;
Fi0 = F_molar * y0;
Cpc = obj.Cpc;
rho = obj.rho;
epsilon = obj.epsilon;
nfe = obj.nfe;
ct0 = 0.001/pr(T0,P,y0);
Cpm0 = get_Cpm(T0,y0);
ci0=ct0.*y0;
uv0 = (epsilon*ct0.*Cpm0+Cpc*rho).*T0;
zspan = 0:z0/(nfe-1):z0;
tspan = 0:10:100;
sol = pdepe(0,@pdefun,@pdeic,@pdebc,zspan,tspan);
a=1;
function [c,f,s] = pdefun(z,t,u,dudx)
c1 = u(1);
c2 = u(2);
c3 = u(3);
c4 = u(4);
uv = u(5);
ct = c1+c2+c3+c4;
y1=c1/ct;
y2=c2/ct;
y3=c3/ct;
y4=c4/ct;
y = [y1,y2,y3,y4];
T = fzero(@solve_T_uv, T0,[],y,uv,ct);
function f0 = solve_T_uv(T,y,uv,ct)
f0 = (epsilon*ct*get_Cpm(T,y)+Cpc*rho)*T-uv;
end
dc1dz = dudx(1);
dc2dz = dudx(2);
dc3dz = dudx(3);
dc4dz = dudx(4);
dTdz = dudx(5);
x = (Fi0(1)-F_molar*y1) / (1-2*y1);
F1 = Fi0(1)-x;
F2 = Fi0(2) -3*x;
F3 = Fi0(3)+2*x;
F4 = Fi0(4);
Ft = F1+F2+F3+F4;
Cpm = get_Cpm(T, y);
h = Ft*Cpm*T;
X = (Fi0(1) – F1) / Fi0(1);
eta = get_eta(T, P, X);
r = get_r(T, P, y, eta, miu,A_for, A_rev, Ea_for, Ea_rev);
r1 = r(1);
r2 = r(2);
r3 = r(3);
r4 = r(4);
Hr = get_Hr(T, P);
c = [1;1;1;1;1];
f = [Da*dc1dz/epsilon;
Da*dc2dz/epsilon;
Da*dc3dz/epsilon;
Da*dc4dz/epsilon;
lambda*dTdz];
s = [r1/epsilon-F1/A/epsilon;
r2/epsilon-F2/A/epsilon;
r3/epsilon-F3/A/epsilon;
r4/epsilon-F4/A/epsilon;
r3*(-Hr)-h/A];
end
function u0 = pdeic(z)
class1 = PFR;
result = class1.reactor_steadystate_model(z);
T_init = result(1);
y_init = result(10:13);
ct_init = 0.001/pr(T_init,P,y_init);
ci_init = ct_init * y_init;
c1_init = ci_init(1);
c2_init = ci_init(2);
c3_init = ci_init(3);
c4_init = ci_init(4);
Cpm_init = get_Cpm(T_init,y_init);
uv_init = (epsilon*ct_init*Cpm_init+Cpc*rho)*T_init;
u0 = [c1_init;c2_init;c3_init;c4_init;uv_init];
end
function [pl,ql,pr,qr] = pdebc(zl,ul,zr,ur,t)
pl = [ul(1)-ci0(1);ul(2)-ci0(2);ul(3)-ci0(3);ul(4)-ci0(4);ul(5)-uv0];
ql = [0;0;0;0;0];
pr = [0;0;0;0;0];
qr = [0;0;0;0;0];
end
endI tried to establish a dynamic model of the reactor using PDEs, but I couldn’t solve it. I don’t know why. The code is as follows:
In this problem, my boundary condition only has the left boundary, which is the u value and dudx when z=0. How should I write it?
function reactor_dynamic_model(obj)
global R miu comp_set A_for A_rev Ea_for Ea_rev Da lambda
Da = 1.8e-4;
lambda = 5e-4;
F_molar = obj.F_molar;
A = obj.cat_vol / obj.z0;
z0 = obj.z0;
y0 = obj.y0;
epsilon = obj.epsilon;
P = obj.P;
T0 = obj.T;
Fi0 = F_molar * y0;
Cpc = obj.Cpc;
rho = obj.rho;
epsilon = obj.epsilon;
nfe = obj.nfe;
ct0 = 0.001/pr(T0,P,y0);
Cpm0 = get_Cpm(T0,y0);
ci0=ct0.*y0;
uv0 = (epsilon*ct0.*Cpm0+Cpc*rho).*T0;
zspan = 0:z0/(nfe-1):z0;
tspan = 0:10:100;
sol = pdepe(0,@pdefun,@pdeic,@pdebc,zspan,tspan);
a=1;
function [c,f,s] = pdefun(z,t,u,dudx)
c1 = u(1);
c2 = u(2);
c3 = u(3);
c4 = u(4);
uv = u(5);
ct = c1+c2+c3+c4;
y1=c1/ct;
y2=c2/ct;
y3=c3/ct;
y4=c4/ct;
y = [y1,y2,y3,y4];
T = fzero(@solve_T_uv, T0,[],y,uv,ct);
function f0 = solve_T_uv(T,y,uv,ct)
f0 = (epsilon*ct*get_Cpm(T,y)+Cpc*rho)*T-uv;
end
dc1dz = dudx(1);
dc2dz = dudx(2);
dc3dz = dudx(3);
dc4dz = dudx(4);
dTdz = dudx(5);
x = (Fi0(1)-F_molar*y1) / (1-2*y1);
F1 = Fi0(1)-x;
F2 = Fi0(2) -3*x;
F3 = Fi0(3)+2*x;
F4 = Fi0(4);
Ft = F1+F2+F3+F4;
Cpm = get_Cpm(T, y);
h = Ft*Cpm*T;
X = (Fi0(1) – F1) / Fi0(1);
eta = get_eta(T, P, X);
r = get_r(T, P, y, eta, miu,A_for, A_rev, Ea_for, Ea_rev);
r1 = r(1);
r2 = r(2);
r3 = r(3);
r4 = r(4);
Hr = get_Hr(T, P);
c = [1;1;1;1;1];
f = [Da*dc1dz/epsilon;
Da*dc2dz/epsilon;
Da*dc3dz/epsilon;
Da*dc4dz/epsilon;
lambda*dTdz];
s = [r1/epsilon-F1/A/epsilon;
r2/epsilon-F2/A/epsilon;
r3/epsilon-F3/A/epsilon;
r4/epsilon-F4/A/epsilon;
r3*(-Hr)-h/A];
end
function u0 = pdeic(z)
class1 = PFR;
result = class1.reactor_steadystate_model(z);
T_init = result(1);
y_init = result(10:13);
ct_init = 0.001/pr(T_init,P,y_init);
ci_init = ct_init * y_init;
c1_init = ci_init(1);
c2_init = ci_init(2);
c3_init = ci_init(3);
c4_init = ci_init(4);
Cpm_init = get_Cpm(T_init,y_init);
uv_init = (epsilon*ct_init*Cpm_init+Cpc*rho)*T_init;
u0 = [c1_init;c2_init;c3_init;c4_init;uv_init];
end
function [pl,ql,pr,qr] = pdebc(zl,ul,zr,ur,t)
pl = [ul(1)-ci0(1);ul(2)-ci0(2);ul(3)-ci0(3);ul(4)-ci0(4);ul(5)-uv0];
ql = [0;0;0;0;0];
pr = [0;0;0;0;0];
qr = [0;0;0;0;0];
end
end I tried to establish a dynamic model of the reactor using PDEs, but I couldn’t solve it. I don’t know why. The code is as follows:
In this problem, my boundary condition only has the left boundary, which is the u value and dudx when z=0. How should I write it?
function reactor_dynamic_model(obj)
global R miu comp_set A_for A_rev Ea_for Ea_rev Da lambda
Da = 1.8e-4;
lambda = 5e-4;
F_molar = obj.F_molar;
A = obj.cat_vol / obj.z0;
z0 = obj.z0;
y0 = obj.y0;
epsilon = obj.epsilon;
P = obj.P;
T0 = obj.T;
Fi0 = F_molar * y0;
Cpc = obj.Cpc;
rho = obj.rho;
epsilon = obj.epsilon;
nfe = obj.nfe;
ct0 = 0.001/pr(T0,P,y0);
Cpm0 = get_Cpm(T0,y0);
ci0=ct0.*y0;
uv0 = (epsilon*ct0.*Cpm0+Cpc*rho).*T0;
zspan = 0:z0/(nfe-1):z0;
tspan = 0:10:100;
sol = pdepe(0,@pdefun,@pdeic,@pdebc,zspan,tspan);
a=1;
function [c,f,s] = pdefun(z,t,u,dudx)
c1 = u(1);
c2 = u(2);
c3 = u(3);
c4 = u(4);
uv = u(5);
ct = c1+c2+c3+c4;
y1=c1/ct;
y2=c2/ct;
y3=c3/ct;
y4=c4/ct;
y = [y1,y2,y3,y4];
T = fzero(@solve_T_uv, T0,[],y,uv,ct);
function f0 = solve_T_uv(T,y,uv,ct)
f0 = (epsilon*ct*get_Cpm(T,y)+Cpc*rho)*T-uv;
end
dc1dz = dudx(1);
dc2dz = dudx(2);
dc3dz = dudx(3);
dc4dz = dudx(4);
dTdz = dudx(5);
x = (Fi0(1)-F_molar*y1) / (1-2*y1);
F1 = Fi0(1)-x;
F2 = Fi0(2) -3*x;
F3 = Fi0(3)+2*x;
F4 = Fi0(4);
Ft = F1+F2+F3+F4;
Cpm = get_Cpm(T, y);
h = Ft*Cpm*T;
X = (Fi0(1) – F1) / Fi0(1);
eta = get_eta(T, P, X);
r = get_r(T, P, y, eta, miu,A_for, A_rev, Ea_for, Ea_rev);
r1 = r(1);
r2 = r(2);
r3 = r(3);
r4 = r(4);
Hr = get_Hr(T, P);
c = [1;1;1;1;1];
f = [Da*dc1dz/epsilon;
Da*dc2dz/epsilon;
Da*dc3dz/epsilon;
Da*dc4dz/epsilon;
lambda*dTdz];
s = [r1/epsilon-F1/A/epsilon;
r2/epsilon-F2/A/epsilon;
r3/epsilon-F3/A/epsilon;
r4/epsilon-F4/A/epsilon;
r3*(-Hr)-h/A];
end
function u0 = pdeic(z)
class1 = PFR;
result = class1.reactor_steadystate_model(z);
T_init = result(1);
y_init = result(10:13);
ct_init = 0.001/pr(T_init,P,y_init);
ci_init = ct_init * y_init;
c1_init = ci_init(1);
c2_init = ci_init(2);
c3_init = ci_init(3);
c4_init = ci_init(4);
Cpm_init = get_Cpm(T_init,y_init);
uv_init = (epsilon*ct_init*Cpm_init+Cpc*rho)*T_init;
u0 = [c1_init;c2_init;c3_init;c4_init;uv_init];
end
function [pl,ql,pr,qr] = pdebc(zl,ul,zr,ur,t)
pl = [ul(1)-ci0(1);ul(2)-ci0(2);ul(3)-ci0(3);ul(4)-ci0(4);ul(5)-uv0];
ql = [0;0;0;0;0];
pr = [0;0;0;0;0];
qr = [0;0;0;0;0];
end
end pdepe, boundary condition, spatial discretization, elliptic, pdes, initial condition, pfr, dynamic model, ammonia synthesis MATLAB Answers — New Questions
y-Label cut off by exportgraphics()
The description of the function exportgraphics says, that it produces a tightly cropped image. In my case it produces a tightly cropped PDF which is unfortunately a little bit too tight at the y-label. I tried rearranging the figure within the window, but exportgraphics does not change its output (which is the intended functionality). Unfortunately, in my case, this leads to a cut image.
The code:
set(groot,’defaultAxesFontName’,’PT Sans’)
p = figure(‘Position’, [455,503,879,400]);
ylabel(‘Label Example: i C’, ‘FontWeight’, ‘bold’)
set(gca, ‘FontSize’, 14)
exportgraphics(p,’label_example.pdf’,’BackgroundColor’,’none’,’ContentType’,’vector’);
produces a slightly cut off y-label as you can see in this picture:
The large C as well as the small i are cut off at the top. The font ‘PT Sans’ is only needed to produce the exact image, but the problem is also happening with other fonts. At the other borders of my figure exportgraphics adds a small white space, as I would expect. This only happens for the y-label.
If you need further information, please comment and I will try to give it to you as soon as possible. Thanks in advance for the help.
Best regards
StefanThe description of the function exportgraphics says, that it produces a tightly cropped image. In my case it produces a tightly cropped PDF which is unfortunately a little bit too tight at the y-label. I tried rearranging the figure within the window, but exportgraphics does not change its output (which is the intended functionality). Unfortunately, in my case, this leads to a cut image.
The code:
set(groot,’defaultAxesFontName’,’PT Sans’)
p = figure(‘Position’, [455,503,879,400]);
ylabel(‘Label Example: i C’, ‘FontWeight’, ‘bold’)
set(gca, ‘FontSize’, 14)
exportgraphics(p,’label_example.pdf’,’BackgroundColor’,’none’,’ContentType’,’vector’);
produces a slightly cut off y-label as you can see in this picture:
The large C as well as the small i are cut off at the top. The font ‘PT Sans’ is only needed to produce the exact image, but the problem is also happening with other fonts. At the other borders of my figure exportgraphics adds a small white space, as I would expect. This only happens for the y-label.
If you need further information, please comment and I will try to give it to you as soon as possible. Thanks in advance for the help.
Best regards
Stefan The description of the function exportgraphics says, that it produces a tightly cropped image. In my case it produces a tightly cropped PDF which is unfortunately a little bit too tight at the y-label. I tried rearranging the figure within the window, but exportgraphics does not change its output (which is the intended functionality). Unfortunately, in my case, this leads to a cut image.
The code:
set(groot,’defaultAxesFontName’,’PT Sans’)
p = figure(‘Position’, [455,503,879,400]);
ylabel(‘Label Example: i C’, ‘FontWeight’, ‘bold’)
set(gca, ‘FontSize’, 14)
exportgraphics(p,’label_example.pdf’,’BackgroundColor’,’none’,’ContentType’,’vector’);
produces a slightly cut off y-label as you can see in this picture:
The large C as well as the small i are cut off at the top. The font ‘PT Sans’ is only needed to produce the exact image, but the problem is also happening with other fonts. At the other borders of my figure exportgraphics adds a small white space, as I would expect. This only happens for the y-label.
If you need further information, please comment and I will try to give it to you as soon as possible. Thanks in advance for the help.
Best regards
Stefan exportgraphics MATLAB Answers — New Questions
accelerate 3D matrix multiptication using bsxfun
Hi
I have athe following code
it takes forever to run, and disable my CPU in the prosess…
I’m looking for a way to make it run better (GPU is also an option)
norm_mode is 256×256 double matrix (X,Y) image
fields is 16384×15 complex double matrix (time,mode)
in the end’ I need a 3D matrix take each norm_mode image will also be a function for time and suming all the modes- 3D matrix 256x256x16384
that is the total_field
do you have any sugestions on how to make it run better?
Thanks,
Barak
function total_field = BuildSpatialField(fields,fiber, sim, others)
total_field = zeros( newSize, newSize, length(others.t) ); % total_field(X,Y,t)
h = waitbar(0, ‘calculate field…’);
for ii=1:others.modes
norm_mode = % load new phi
tmp = bsxfun(@times, norm_mode, reshape(fields(:,ii),1,1,[]));
total_field = total_field + tmp;
waitbar(ii/others.modes, h, [‘mode ‘ num2str(ii) ‘ from ‘ num2str(others.modes)]);
end
close(h);
endHi
I have athe following code
it takes forever to run, and disable my CPU in the prosess…
I’m looking for a way to make it run better (GPU is also an option)
norm_mode is 256×256 double matrix (X,Y) image
fields is 16384×15 complex double matrix (time,mode)
in the end’ I need a 3D matrix take each norm_mode image will also be a function for time and suming all the modes- 3D matrix 256x256x16384
that is the total_field
do you have any sugestions on how to make it run better?
Thanks,
Barak
function total_field = BuildSpatialField(fields,fiber, sim, others)
total_field = zeros( newSize, newSize, length(others.t) ); % total_field(X,Y,t)
h = waitbar(0, ‘calculate field…’);
for ii=1:others.modes
norm_mode = % load new phi
tmp = bsxfun(@times, norm_mode, reshape(fields(:,ii),1,1,[]));
total_field = total_field + tmp;
waitbar(ii/others.modes, h, [‘mode ‘ num2str(ii) ‘ from ‘ num2str(others.modes)]);
end
close(h);
end Hi
I have athe following code
it takes forever to run, and disable my CPU in the prosess…
I’m looking for a way to make it run better (GPU is also an option)
norm_mode is 256×256 double matrix (X,Y) image
fields is 16384×15 complex double matrix (time,mode)
in the end’ I need a 3D matrix take each norm_mode image will also be a function for time and suming all the modes- 3D matrix 256x256x16384
that is the total_field
do you have any sugestions on how to make it run better?
Thanks,
Barak
function total_field = BuildSpatialField(fields,fiber, sim, others)
total_field = zeros( newSize, newSize, length(others.t) ); % total_field(X,Y,t)
h = waitbar(0, ‘calculate field…’);
for ii=1:others.modes
norm_mode = % load new phi
tmp = bsxfun(@times, norm_mode, reshape(fields(:,ii),1,1,[]));
total_field = total_field + tmp;
waitbar(ii/others.modes, h, [‘mode ‘ num2str(ii) ‘ from ‘ num2str(others.modes)]);
end
close(h);
end bsxfun, acceleration MATLAB Answers — New Questions
how to close an UIFigure from a second app
Hello !
Using app designer, I’ve created an application (app1) which, when I press a button (button1), opens a second application (app2). In app1, I also have a second button (button2) which, when pressed, opens a third app (app3). In app3, I have a button which, when pressed, executes a code and when it’s finished, I’d like the code to close not only app3 but also app2. I’ve managed to close app3, but my problem is with closing app2.
In app1 and app3, I declared the app2 handle (app2handle) as a public property. However, in app3, when I use the line:
delete(app.app2handle);
app2 doesn’t close.
The same line of code in app1, closes app2.
How can I do this?Hello !
Using app designer, I’ve created an application (app1) which, when I press a button (button1), opens a second application (app2). In app1, I also have a second button (button2) which, when pressed, opens a third app (app3). In app3, I have a button which, when pressed, executes a code and when it’s finished, I’d like the code to close not only app3 but also app2. I’ve managed to close app3, but my problem is with closing app2.
In app1 and app3, I declared the app2 handle (app2handle) as a public property. However, in app3, when I use the line:
delete(app.app2handle);
app2 doesn’t close.
The same line of code in app1, closes app2.
How can I do this? Hello !
Using app designer, I’ve created an application (app1) which, when I press a button (button1), opens a second application (app2). In app1, I also have a second button (button2) which, when pressed, opens a third app (app3). In app3, I have a button which, when pressed, executes a code and when it’s finished, I’d like the code to close not only app3 but also app2. I’ve managed to close app3, but my problem is with closing app2.
In app1 and app3, I declared the app2 handle (app2handle) as a public property. However, in app3, when I use the line:
delete(app.app2handle);
app2 doesn’t close.
The same line of code in app1, closes app2.
How can I do this? appdesigner MATLAB Answers — New Questions
How to solve this differential equation? I got ”Warning: Unable to find symbolic solution.” error. What am I missing ?
T=200;
W=37.65;
W=17.4;
FC=400/240/2;
d0=0.005;
C=0.16099608;
curve=10;
hmax=30e3;
g0=9.8;
R=315000;
syms H(t)
ode =diff(H,t,2) == -d0*(exp(-curve*H/hmax)-exp(-curve))*C*diff(H,t,1)^2/(W-t*FC)*9.8-g0/(1+H/R)^2+T/(W-t*FC)*9.8;
hSol(t) = dsolve(ode)T=200;
W=37.65;
W=17.4;
FC=400/240/2;
d0=0.005;
C=0.16099608;
curve=10;
hmax=30e3;
g0=9.8;
R=315000;
syms H(t)
ode =diff(H,t,2) == -d0*(exp(-curve*H/hmax)-exp(-curve))*C*diff(H,t,1)^2/(W-t*FC)*9.8-g0/(1+H/R)^2+T/(W-t*FC)*9.8;
hSol(t) = dsolve(ode) T=200;
W=37.65;
W=17.4;
FC=400/240/2;
d0=0.005;
C=0.16099608;
curve=10;
hmax=30e3;
g0=9.8;
R=315000;
syms H(t)
ode =diff(H,t,2) == -d0*(exp(-curve*H/hmax)-exp(-curve))*C*diff(H,t,1)^2/(W-t*FC)*9.8-g0/(1+H/R)^2+T/(W-t*FC)*9.8;
hSol(t) = dsolve(ode) differential equations, code MATLAB Answers — New Questions
Boundary Extraction Issue Using Image Restoration Techniques
Hi, I’m working on extracting boundary points from an image, but the output appears inverted. How can I correctly extract boundary points so the image restoration matches the original orientation? Here’s my current MATLAB code:
clear all;
img = imread(‘parol.png’); % Replace with your image file
grayImg = rgb2gray(img); % Convert to grayscale
bwImg = imbinarize(grayImg); % Convert to binary image
% Invert the binary image if necessary
bwImg = imcomplement(bwImg);
% Fill holes in the binary image
bwImg = imfill(bwImg, ‘holes’);
% Get boundary points
boundaries = bwboundaries(bwImg);
% Extract the main boundary points
boundaryPoints = boundaries{1};
% Plot the boundary points
figure;
subplot(1,2,1)
imshow(img);
subplot(1,2,2)
plot(boundaryPoints(:,2), boundaryPoints(:,1), ‘r’, ‘LineWidth’, 2);
hold off;
daspect([1 1 1]);
Thanks!Hi, I’m working on extracting boundary points from an image, but the output appears inverted. How can I correctly extract boundary points so the image restoration matches the original orientation? Here’s my current MATLAB code:
clear all;
img = imread(‘parol.png’); % Replace with your image file
grayImg = rgb2gray(img); % Convert to grayscale
bwImg = imbinarize(grayImg); % Convert to binary image
% Invert the binary image if necessary
bwImg = imcomplement(bwImg);
% Fill holes in the binary image
bwImg = imfill(bwImg, ‘holes’);
% Get boundary points
boundaries = bwboundaries(bwImg);
% Extract the main boundary points
boundaryPoints = boundaries{1};
% Plot the boundary points
figure;
subplot(1,2,1)
imshow(img);
subplot(1,2,2)
plot(boundaryPoints(:,2), boundaryPoints(:,1), ‘r’, ‘LineWidth’, 2);
hold off;
daspect([1 1 1]);
Thanks! Hi, I’m working on extracting boundary points from an image, but the output appears inverted. How can I correctly extract boundary points so the image restoration matches the original orientation? Here’s my current MATLAB code:
clear all;
img = imread(‘parol.png’); % Replace with your image file
grayImg = rgb2gray(img); % Convert to grayscale
bwImg = imbinarize(grayImg); % Convert to binary image
% Invert the binary image if necessary
bwImg = imcomplement(bwImg);
% Fill holes in the binary image
bwImg = imfill(bwImg, ‘holes’);
% Get boundary points
boundaries = bwboundaries(bwImg);
% Extract the main boundary points
boundaryPoints = boundaries{1};
% Plot the boundary points
figure;
subplot(1,2,1)
imshow(img);
subplot(1,2,2)
plot(boundaryPoints(:,2), boundaryPoints(:,1), ‘r’, ‘LineWidth’, 2);
hold off;
daspect([1 1 1]);
Thanks! image processing, boundary point MATLAB Answers — New Questions
Make a plot with gradient shaded confidence intervals
Dear all,
I would like to fill the red shaded area in the plot below using a gradient fill (ideally with a jet colormap), so that the area between the blue line and the upper red dashed line goes from red to blue and the area between the blue line and the lower red dashed line goes from red to blue.
x = Data(:,1);
y = Data(:,2);
y10 = Data(:,3);
y90 = Data(:,4);
figure
plot(x, y, ‘b’, ‘LineWidth’, 2);
hold on;
plot(x, y10, ‘–r’);
plot(x, y90, ‘–r’);
fill([x; flipud(x)], [y10; flipud(y90)], ‘r’,…
‘FaceAlpha’, 0.1, ‘EdgeColor’, ‘none’);
hold off;
legend(‘Median’, ’10th-90th Percentiles’, ‘Location’, ‘best’);
grid on;
Just to give an idea, something like this (without color steps but with a gradient fill):
I have tried myself, but I can’t really find a nice (and fast) solution.
I have attched the Data file.
Any help would be grately appreciated!Dear all,
I would like to fill the red shaded area in the plot below using a gradient fill (ideally with a jet colormap), so that the area between the blue line and the upper red dashed line goes from red to blue and the area between the blue line and the lower red dashed line goes from red to blue.
x = Data(:,1);
y = Data(:,2);
y10 = Data(:,3);
y90 = Data(:,4);
figure
plot(x, y, ‘b’, ‘LineWidth’, 2);
hold on;
plot(x, y10, ‘–r’);
plot(x, y90, ‘–r’);
fill([x; flipud(x)], [y10; flipud(y90)], ‘r’,…
‘FaceAlpha’, 0.1, ‘EdgeColor’, ‘none’);
hold off;
legend(‘Median’, ’10th-90th Percentiles’, ‘Location’, ‘best’);
grid on;
Just to give an idea, something like this (without color steps but with a gradient fill):
I have tried myself, but I can’t really find a nice (and fast) solution.
I have attched the Data file.
Any help would be grately appreciated! Dear all,
I would like to fill the red shaded area in the plot below using a gradient fill (ideally with a jet colormap), so that the area between the blue line and the upper red dashed line goes from red to blue and the area between the blue line and the lower red dashed line goes from red to blue.
x = Data(:,1);
y = Data(:,2);
y10 = Data(:,3);
y90 = Data(:,4);
figure
plot(x, y, ‘b’, ‘LineWidth’, 2);
hold on;
plot(x, y10, ‘–r’);
plot(x, y90, ‘–r’);
fill([x; flipud(x)], [y10; flipud(y90)], ‘r’,…
‘FaceAlpha’, 0.1, ‘EdgeColor’, ‘none’);
hold off;
legend(‘Median’, ’10th-90th Percentiles’, ‘Location’, ‘best’);
grid on;
Just to give an idea, something like this (without color steps but with a gradient fill):
I have tried myself, but I can’t really find a nice (and fast) solution.
I have attched the Data file.
Any help would be grately appreciated! plotting, plot, colormap, graph, graphics, matlab MATLAB Answers — New Questions
Coloring area between two graphs in specific interval
Hi,
I have a plot that looks as shown and would like to color specific areas of it. In the blue graph, I would like shade the area of the minimum, so roughly between 100 < x < 115 and up to y = 0.45 (the area below the baseline).
I had no success doing this using a linear function and the inBetween and fill functions.
Thanks a lot,
ChrisHi,
I have a plot that looks as shown and would like to color specific areas of it. In the blue graph, I would like shade the area of the minimum, so roughly between 100 < x < 115 and up to y = 0.45 (the area below the baseline).
I had no success doing this using a linear function and the inBetween and fill functions.
Thanks a lot,
Chris Hi,
I have a plot that looks as shown and would like to color specific areas of it. In the blue graph, I would like shade the area of the minimum, so roughly between 100 < x < 115 and up to y = 0.45 (the area below the baseline).
I had no success doing this using a linear function and the inBetween and fill functions.
Thanks a lot,
Chris integral, area coloring MATLAB Answers — New Questions
I want to calculate basic reproduction number, how to make 6×6 matrix and also how to add different parameter
My basic reproduction number includes certain mathematical symbols which I am not able to write in matlab programming, how will I be able to calculate basic reproduction number? In other words how to code R0My basic reproduction number includes certain mathematical symbols which I am not able to write in matlab programming, how will I be able to calculate basic reproduction number? In other words how to code R0 My basic reproduction number includes certain mathematical symbols which I am not able to write in matlab programming, how will I be able to calculate basic reproduction number? In other words how to code R0 basic reproduction number MATLAB Answers — New Questions
about the starData.mat
Can anyone please tell how to download starData.mat file on pc. and please post the content of that file if possible I am unable to download it.Can anyone please tell how to download starData.mat file on pc. and please post the content of that file if possible I am unable to download it. Can anyone please tell how to download starData.mat file on pc. and please post the content of that file if possible I am unable to download it. stardata MATLAB Answers — New Questions
Problem with legend: colors don’t match value of variable
hello, i have a problem with the legend in this script: i am doing subplots for the two parameters e and t, in every subplot e is fixed while t can vary between 5 different values. the problem is the legend doesn’t match the colors. for instance, t= -50 should be red, -25 green, 0 blue, 25 light blue and t = 50 black, but the legend doesnt say this. Could anybody give me any suggestions on how to index the color and the value of the parameter so that they match? thank you
here is the code:
clear; clc; clf;
n = 1001;
x = linspace(-100, 100, n);
y = linspace(-100, 100, n);
[X, Y] = meshgrid(x, y);
clr = [‘r’,’g’,’b’,’c’,’k’];
a = 4;
e = [-50 -10 0 50];
t = linspace(-50, 50, 5);
LegendsStrings = cell(length(t),1);
for q = 1:a
for r = 1:5
Z = fn(X, Y, e(q), t(r), n);
subplot(2, 2, q)
xline(0, ‘Color’, ‘k’, ‘LineWidth’, 0.5);
yline(0, ‘Color’, ‘k’, ‘LineWidth’, 0.5);
hold on
v = [0, 0];
contour(X, Y, Z, v, ‘LineWidth’, 1.5, ‘LineColor’, clr(r))
xlabel(‘x’)
ylabel(‘y’)
title("e = " + e(q))
grid on
axis equal
hold off
LegendsStrings{r} = [‘t = ‘, num2str(t(r))];
end
legend(LegendsStrings, ‘Interpreter’, ‘none’)
end
function Z = fn(X,Y,e,t,n)
Z = zeros(n, n);
B = X + Y + e + t;
D = X.*Y – e.*t;
for i= 1:n
for j= 1:n
if B(i,j) >= 0
Z(i,j) = D(i,j);
else
Z(i,j) = -1;
end
end
end
endhello, i have a problem with the legend in this script: i am doing subplots for the two parameters e and t, in every subplot e is fixed while t can vary between 5 different values. the problem is the legend doesn’t match the colors. for instance, t= -50 should be red, -25 green, 0 blue, 25 light blue and t = 50 black, but the legend doesnt say this. Could anybody give me any suggestions on how to index the color and the value of the parameter so that they match? thank you
here is the code:
clear; clc; clf;
n = 1001;
x = linspace(-100, 100, n);
y = linspace(-100, 100, n);
[X, Y] = meshgrid(x, y);
clr = [‘r’,’g’,’b’,’c’,’k’];
a = 4;
e = [-50 -10 0 50];
t = linspace(-50, 50, 5);
LegendsStrings = cell(length(t),1);
for q = 1:a
for r = 1:5
Z = fn(X, Y, e(q), t(r), n);
subplot(2, 2, q)
xline(0, ‘Color’, ‘k’, ‘LineWidth’, 0.5);
yline(0, ‘Color’, ‘k’, ‘LineWidth’, 0.5);
hold on
v = [0, 0];
contour(X, Y, Z, v, ‘LineWidth’, 1.5, ‘LineColor’, clr(r))
xlabel(‘x’)
ylabel(‘y’)
title("e = " + e(q))
grid on
axis equal
hold off
LegendsStrings{r} = [‘t = ‘, num2str(t(r))];
end
legend(LegendsStrings, ‘Interpreter’, ‘none’)
end
function Z = fn(X,Y,e,t,n)
Z = zeros(n, n);
B = X + Y + e + t;
D = X.*Y – e.*t;
for i= 1:n
for j= 1:n
if B(i,j) >= 0
Z(i,j) = D(i,j);
else
Z(i,j) = -1;
end
end
end
end hello, i have a problem with the legend in this script: i am doing subplots for the two parameters e and t, in every subplot e is fixed while t can vary between 5 different values. the problem is the legend doesn’t match the colors. for instance, t= -50 should be red, -25 green, 0 blue, 25 light blue and t = 50 black, but the legend doesnt say this. Could anybody give me any suggestions on how to index the color and the value of the parameter so that they match? thank you
here is the code:
clear; clc; clf;
n = 1001;
x = linspace(-100, 100, n);
y = linspace(-100, 100, n);
[X, Y] = meshgrid(x, y);
clr = [‘r’,’g’,’b’,’c’,’k’];
a = 4;
e = [-50 -10 0 50];
t = linspace(-50, 50, 5);
LegendsStrings = cell(length(t),1);
for q = 1:a
for r = 1:5
Z = fn(X, Y, e(q), t(r), n);
subplot(2, 2, q)
xline(0, ‘Color’, ‘k’, ‘LineWidth’, 0.5);
yline(0, ‘Color’, ‘k’, ‘LineWidth’, 0.5);
hold on
v = [0, 0];
contour(X, Y, Z, v, ‘LineWidth’, 1.5, ‘LineColor’, clr(r))
xlabel(‘x’)
ylabel(‘y’)
title("e = " + e(q))
grid on
axis equal
hold off
LegendsStrings{r} = [‘t = ‘, num2str(t(r))];
end
legend(LegendsStrings, ‘Interpreter’, ‘none’)
end
function Z = fn(X,Y,e,t,n)
Z = zeros(n, n);
B = X + Y + e + t;
D = X.*Y – e.*t;
for i= 1:n
for j= 1:n
if B(i,j) >= 0
Z(i,j) = D(i,j);
else
Z(i,j) = -1;
end
end
end
end legend, colorstring, indexing, plot, subplot, color MATLAB Answers — New Questions
Calculating with different date times
Hi,
I have one array that contains my speed during running and the corresponding time it was recorded at. Measurements are about every one second.
Then I have a second array that contains my heart rate measurements and the corresponding time they were recorded at. These measurements contain data from about every 6-10 seconds.
This is shown by the exemplary screenshot.
What I am trying to do is divide my speed by heart rate at the corresponding times to get something like ‘performance’. So I need to average the speed of all measurements that were done for one measurement of heart rate. Something like this:
mean(Speed(1:6))/Heartrate(1)
mean(Speed(7:16))/Heartrate(2)
I cannot do this by hand for the amount of data but have problems coming up with code that selects the correct time range to average the speed (the time range that corresponds to every one measurement of heart rate) and then divides these values.
I appreciate your ideas/ help!
PS Sorry for the bad question title, I couldn’t think of a better one :/Hi,
I have one array that contains my speed during running and the corresponding time it was recorded at. Measurements are about every one second.
Then I have a second array that contains my heart rate measurements and the corresponding time they were recorded at. These measurements contain data from about every 6-10 seconds.
This is shown by the exemplary screenshot.
What I am trying to do is divide my speed by heart rate at the corresponding times to get something like ‘performance’. So I need to average the speed of all measurements that were done for one measurement of heart rate. Something like this:
mean(Speed(1:6))/Heartrate(1)
mean(Speed(7:16))/Heartrate(2)
I cannot do this by hand for the amount of data but have problems coming up with code that selects the correct time range to average the speed (the time range that corresponds to every one measurement of heart rate) and then divides these values.
I appreciate your ideas/ help!
PS Sorry for the bad question title, I couldn’t think of a better one :/ Hi,
I have one array that contains my speed during running and the corresponding time it was recorded at. Measurements are about every one second.
Then I have a second array that contains my heart rate measurements and the corresponding time they were recorded at. These measurements contain data from about every 6-10 seconds.
This is shown by the exemplary screenshot.
What I am trying to do is divide my speed by heart rate at the corresponding times to get something like ‘performance’. So I need to average the speed of all measurements that were done for one measurement of heart rate. Something like this:
mean(Speed(1:6))/Heartrate(1)
mean(Speed(7:16))/Heartrate(2)
I cannot do this by hand for the amount of data but have problems coming up with code that selects the correct time range to average the speed (the time range that corresponds to every one measurement of heart rate) and then divides these values.
I appreciate your ideas/ help!
PS Sorry for the bad question title, I couldn’t think of a better one :/ datetime MATLAB Answers — New Questions
Undefined function ‘dts_cast_with_warning’ for input arguments of type ‘matlab.ui.Figure’.
Hello,
I’m implementing a function in mex file (on GPU) in MALTLAB. But I faced with the following error in MATLAB:
Undefined function ‘dts_cast_with_warning’ for input arguments of type ‘matlab.ui.Figure’.
Error in BPmimo2C (line 98)
figure
Error in test (line 29)
BPmimo2C_mex( Efield, f_par, xyz_par, TX, TY, numT, numR)
Here is where this error occurred (in the "figure" part):
…
…
…
for j = 1:numel(z)
hf(j,:,:) = z(j);
end
figure %************** here is where the error occurred
% figure(1);
er = squeeze(max(image,[],1));
h = surf(squeeze(uf(1,:,:)),squeeze(vf(1,:,:)),er);
colormap(jet);
set(h,’LineStyle’,’none’);
view(2);
Please help me solve this problem.
Thank youHello,
I’m implementing a function in mex file (on GPU) in MALTLAB. But I faced with the following error in MATLAB:
Undefined function ‘dts_cast_with_warning’ for input arguments of type ‘matlab.ui.Figure’.
Error in BPmimo2C (line 98)
figure
Error in test (line 29)
BPmimo2C_mex( Efield, f_par, xyz_par, TX, TY, numT, numR)
Here is where this error occurred (in the "figure" part):
…
…
…
for j = 1:numel(z)
hf(j,:,:) = z(j);
end
figure %************** here is where the error occurred
% figure(1);
er = squeeze(max(image,[],1));
h = surf(squeeze(uf(1,:,:)),squeeze(vf(1,:,:)),er);
colormap(jet);
set(h,’LineStyle’,’none’);
view(2);
Please help me solve this problem.
Thank you Hello,
I’m implementing a function in mex file (on GPU) in MALTLAB. But I faced with the following error in MATLAB:
Undefined function ‘dts_cast_with_warning’ for input arguments of type ‘matlab.ui.Figure’.
Error in BPmimo2C (line 98)
figure
Error in test (line 29)
BPmimo2C_mex( Efield, f_par, xyz_par, TX, TY, numT, numR)
Here is where this error occurred (in the "figure" part):
…
…
…
for j = 1:numel(z)
hf(j,:,:) = z(j);
end
figure %************** here is where the error occurred
% figure(1);
er = squeeze(max(image,[],1));
h = surf(squeeze(uf(1,:,:)),squeeze(vf(1,:,:)),er);
colormap(jet);
set(h,’LineStyle’,’none’);
view(2);
Please help me solve this problem.
Thank you matlab, gpu, figure, mex MATLAB Answers — New Questions
Cannot register Custom Add-on Library
I’ve written a custom add-on library to controll a stepper motor, however I’m having trouble registering it. I’ve both added the path via Set Path and tried running >>addpath(‘C:Users…’), but when I type in >>listArduinoLibraries, the add-on doesn’t show up.
The Current Folder browser looks excactly as shown here (https://de.mathworks.com/help/matlab/supportpkg/register-add-on-library.html), with only the src folder and its content appearing faded.
This is my C++ Header File:
# include "LibraryBase.h"
# include "Stepper.h"
// Command IDs
# define ROTATE_STEPPER 0x01
# define STEPPER_SPEED 0x02
class Stepper_L298N : public LibraryBase
{
private:
Stepper stepper;
public:
Stepper_L298N(MWArduinoClass& a, int stepsPerRevolution, int pin1, int pin2, int pin3, int pin4)
: stepper (stepsPerRevolution, pin1, pin2, pin3, pin4)
{
libName = "Stepper_L298N";
a.registerLibrary(this);
}
public:
void commandHandler(byte cmdID, byte* dataIn, unsigned int payloadSize)
{
switch (cmdID){
case ROTATE_STEPPER:{
int steps;
memcpy(&steps, dataIn, sizeof(int));
stepper.step(steps);
sendResponseMsg(cmdID, 0, 0);
break;
}
case STEPPER_SPEED:{
int speed;
memcpy(&speed, dataIn, sizeof(int));
stepper.setSpeed(speed);
sendResponseMsg(cmdID, 0, 0);
break;
}
default:{
}
}
}
};
The first part of my Add-On Class looks like this:
classdef Stepper_L298N < matlabshared.addon.LibraryBase
properties (Access = protected)
Pins
end
properties (Access = private, Constant = true)
ROTATE_STEPPER = hex2dec(’01’)
STEPPER_SPEED = hex2dec(’02’)
end
properties (Access = protected, Constant = true)
LibraryName = ‘Stepper_L298N’
DependentLibraries = {}
LibraryHeaderFiles = ‘Stepper.h’
CppHeaderFile = fullfile(arduinoio.FilePath(mfilename(‘fullpath’)), ‘src’, ‘Stepper_L298N.h’)
CppClassName = ‘Stepper_L298N’
end
I’ve checked for typos in the file names, but I can’t find any. Any advice what else could be wrong?I’ve written a custom add-on library to controll a stepper motor, however I’m having trouble registering it. I’ve both added the path via Set Path and tried running >>addpath(‘C:Users…’), but when I type in >>listArduinoLibraries, the add-on doesn’t show up.
The Current Folder browser looks excactly as shown here (https://de.mathworks.com/help/matlab/supportpkg/register-add-on-library.html), with only the src folder and its content appearing faded.
This is my C++ Header File:
# include "LibraryBase.h"
# include "Stepper.h"
// Command IDs
# define ROTATE_STEPPER 0x01
# define STEPPER_SPEED 0x02
class Stepper_L298N : public LibraryBase
{
private:
Stepper stepper;
public:
Stepper_L298N(MWArduinoClass& a, int stepsPerRevolution, int pin1, int pin2, int pin3, int pin4)
: stepper (stepsPerRevolution, pin1, pin2, pin3, pin4)
{
libName = "Stepper_L298N";
a.registerLibrary(this);
}
public:
void commandHandler(byte cmdID, byte* dataIn, unsigned int payloadSize)
{
switch (cmdID){
case ROTATE_STEPPER:{
int steps;
memcpy(&steps, dataIn, sizeof(int));
stepper.step(steps);
sendResponseMsg(cmdID, 0, 0);
break;
}
case STEPPER_SPEED:{
int speed;
memcpy(&speed, dataIn, sizeof(int));
stepper.setSpeed(speed);
sendResponseMsg(cmdID, 0, 0);
break;
}
default:{
}
}
}
};
The first part of my Add-On Class looks like this:
classdef Stepper_L298N < matlabshared.addon.LibraryBase
properties (Access = protected)
Pins
end
properties (Access = private, Constant = true)
ROTATE_STEPPER = hex2dec(’01’)
STEPPER_SPEED = hex2dec(’02’)
end
properties (Access = protected, Constant = true)
LibraryName = ‘Stepper_L298N’
DependentLibraries = {}
LibraryHeaderFiles = ‘Stepper.h’
CppHeaderFile = fullfile(arduinoio.FilePath(mfilename(‘fullpath’)), ‘src’, ‘Stepper_L298N.h’)
CppClassName = ‘Stepper_L298N’
end
I’ve checked for typos in the file names, but I can’t find any. Any advice what else could be wrong? I’ve written a custom add-on library to controll a stepper motor, however I’m having trouble registering it. I’ve both added the path via Set Path and tried running >>addpath(‘C:Users…’), but when I type in >>listArduinoLibraries, the add-on doesn’t show up.
The Current Folder browser looks excactly as shown here (https://de.mathworks.com/help/matlab/supportpkg/register-add-on-library.html), with only the src folder and its content appearing faded.
This is my C++ Header File:
# include "LibraryBase.h"
# include "Stepper.h"
// Command IDs
# define ROTATE_STEPPER 0x01
# define STEPPER_SPEED 0x02
class Stepper_L298N : public LibraryBase
{
private:
Stepper stepper;
public:
Stepper_L298N(MWArduinoClass& a, int stepsPerRevolution, int pin1, int pin2, int pin3, int pin4)
: stepper (stepsPerRevolution, pin1, pin2, pin3, pin4)
{
libName = "Stepper_L298N";
a.registerLibrary(this);
}
public:
void commandHandler(byte cmdID, byte* dataIn, unsigned int payloadSize)
{
switch (cmdID){
case ROTATE_STEPPER:{
int steps;
memcpy(&steps, dataIn, sizeof(int));
stepper.step(steps);
sendResponseMsg(cmdID, 0, 0);
break;
}
case STEPPER_SPEED:{
int speed;
memcpy(&speed, dataIn, sizeof(int));
stepper.setSpeed(speed);
sendResponseMsg(cmdID, 0, 0);
break;
}
default:{
}
}
}
};
The first part of my Add-On Class looks like this:
classdef Stepper_L298N < matlabshared.addon.LibraryBase
properties (Access = protected)
Pins
end
properties (Access = private, Constant = true)
ROTATE_STEPPER = hex2dec(’01’)
STEPPER_SPEED = hex2dec(’02’)
end
properties (Access = protected, Constant = true)
LibraryName = ‘Stepper_L298N’
DependentLibraries = {}
LibraryHeaderFiles = ‘Stepper.h’
CppHeaderFile = fullfile(arduinoio.FilePath(mfilename(‘fullpath’)), ‘src’, ‘Stepper_L298N.h’)
CppClassName = ‘Stepper_L298N’
end
I’ve checked for typos in the file names, but I can’t find any. Any advice what else could be wrong? arduino, add-on MATLAB Answers — New Questions
Where do I install third-pary libraries?
I’ve written a custom add-on library, that includes the ‘Stepper.h’ library from Arduino. I have definitely downloaded and installed the Arduino library and can access it from the Arduino IDE just fine. However, when I try running my code in Matlab, I get the following error:
"The Arduino source ‘Stepper.h’ for libraries ‘StepperL298N/Stepper_L298N’ cannot be found. Install the third party library source first and try again."
I know where the library is and have added the path manually, but will still get he same error. When I enter >>fullfile(arduinoio.CLIRoot, ‘user’,’libraries’) to find out where to move the library so that Matlab can find it, I get the following error:
"Unable to resolve the name ‘arduinoio.CLIRoot’"
Can someone tell me how to resolve this?I’ve written a custom add-on library, that includes the ‘Stepper.h’ library from Arduino. I have definitely downloaded and installed the Arduino library and can access it from the Arduino IDE just fine. However, when I try running my code in Matlab, I get the following error:
"The Arduino source ‘Stepper.h’ for libraries ‘StepperL298N/Stepper_L298N’ cannot be found. Install the third party library source first and try again."
I know where the library is and have added the path manually, but will still get he same error. When I enter >>fullfile(arduinoio.CLIRoot, ‘user’,’libraries’) to find out where to move the library so that Matlab can find it, I get the following error:
"Unable to resolve the name ‘arduinoio.CLIRoot’"
Can someone tell me how to resolve this? I’ve written a custom add-on library, that includes the ‘Stepper.h’ library from Arduino. I have definitely downloaded and installed the Arduino library and can access it from the Arduino IDE just fine. However, when I try running my code in Matlab, I get the following error:
"The Arduino source ‘Stepper.h’ for libraries ‘StepperL298N/Stepper_L298N’ cannot be found. Install the third party library source first and try again."
I know where the library is and have added the path manually, but will still get he same error. When I enter >>fullfile(arduinoio.CLIRoot, ‘user’,’libraries’) to find out where to move the library so that Matlab can find it, I get the following error:
"Unable to resolve the name ‘arduinoio.CLIRoot’"
Can someone tell me how to resolve this? custom add-on library, third-party libraries MATLAB Answers — New Questions
Based on the surf algorithm, I am stitching three images with overlapping regions.
Based on the surf algorithm, I am stitching three images with overlapping regions. I have code that successfully stitches img1 and img2 into result1, and img2 and img3 into result2. However, when I attempt to stitch result1 and result2 together, issues arise. How should I proceed?
基于surf算法,拼接三张具有重叠区域的图像。我有拼接两张的代码,问题是三张图片img1,img2,img3,img1和img2有重叠区域,img2和img3有重叠区域。img1和img2拼接没有问题,结果是result1,img2和img3拼接没有问题,结果是result2,但将result1和result2拼接时却出现了问题,我该怎么办Based on the surf algorithm, I am stitching three images with overlapping regions. I have code that successfully stitches img1 and img2 into result1, and img2 and img3 into result2. However, when I attempt to stitch result1 and result2 together, issues arise. How should I proceed?
基于surf算法,拼接三张具有重叠区域的图像。我有拼接两张的代码,问题是三张图片img1,img2,img3,img1和img2有重叠区域,img2和img3有重叠区域。img1和img2拼接没有问题,结果是result1,img2和img3拼接没有问题,结果是result2,但将result1和result2拼接时却出现了问题,我该怎么办 Based on the surf algorithm, I am stitching three images with overlapping regions. I have code that successfully stitches img1 and img2 into result1, and img2 and img3 into result2. However, when I attempt to stitch result1 and result2 together, issues arise. How should I proceed?
基于surf算法,拼接三张具有重叠区域的图像。我有拼接两张的代码,问题是三张图片img1,img2,img3,img1和img2有重叠区域,img2和img3有重叠区域。img1和img2拼接没有问题,结果是result1,img2和img3拼接没有问题,结果是result2,但将result1和result2拼接时却出现了问题,我该怎么办 surf, image stiching MATLAB Answers — New Questions
Rectify image with known coordinates
I have calibration images from four cameras in a 1×4 array. For simplicity, I’m showing the processing for just one camera. The image shows a calibration plate that contains black dots at a know separation.
The image is in pixels. I already post processes the image and obtain a rectification mapping function image coordinates to world coordinates. I input the location of known location in the image in image coordinates and the coordinates of these points in the real world. The output is the function that can take any point in image coordinates and convert it to real world coordinates.
I can plot the points in real world coordinates using the ‘scatter function’ (as in the figure below) but I’m having problems rectifying the whole image. I have no idea how to reshape the image with the new real world coordinates.
Here’s the data and code (data was a bit over 5MB).
clear
clc
close all
% I = points in image coordinates
% W = same points as in I but in real world coordinates
% calImg = Raw image in image coordinates
load("calImg.mat")
rectify_quad = cell(length(calImg),1); imagePoints2 = rectify_quad;
% CALIBRATION: compute the image rectification function for this camera
% from the world coords and image coords of the calibration dots (using
% a quadratic transformation: order = 2)
trans_order = 2;
rectify_quad{1} = calibrate_camera(I,W,trans_order);
imagePoints2{1} = rectify_quad{1}(I);
figure(1)
subplot(121)
imagesc(calImg); hold on
colormap(gray)
scatter(I(:,1), I(:,2), ‘r’, ‘LineWidth’,1)
axis equal
xlabel(‘pixel’)
ylabel(‘pixel’)
title(‘Image and detected points (Image coord)’)
subplot(122)
% I want to plot the rectified image in world coordinates similar to subplot (121)
scatter(imagePoints2{1}(:,1),imagePoints2{1}(:,2), ‘r’, ‘LineWidth’,1);
axis equal
xlabel(‘meters’)
ylabel(‘meters’)
title(‘detected points (real world coord)’)
function rectify = calibrate_camera(I,W,order)
% calculate the transformation function to convert image coordinates to
% world (physical) coordinates, including calibration (converting pixels to
% meters), undistortion, and rectification
%
% rectify: function handle to map image coordinates to world coordinates
% I: set of known calibration points in image coordinates (n x 2 vector) [px]
% W: set of known calibration points in world coordinates (n x 2 vector) [m]
% order: 1 for linear transformation (corrects for camera viewing angle but not lens distortion),
% 2 for quadratic transformation (corrects for camera viewing angle and lens distortion)
%
% references: Fujita et al 1998 (Water Res.), Creutin et al 2003 (J. Hydrol.)
%
% to use function handle: points_m = rectify(points_px)
% points_px: set of points in image coordinates (m x 2 vector) [px]
% points_m: set of points converted to world coordinates (m x 2 vector) [px]
% find transformation coefficients
if order == 2
A = [I.^2, I, ones(size(I,1),1), -I(:,1).^2.*W(:,1), -I(:,2).^2.*W(:,1), -I(:,1).*W(:,1), -I(:,2).*W(:,1), zeros(size(I,1),5);
zeros(size(I,1),5), -I(:,1).^2.*W(:,2), -I(:,2).^2.*W(:,2), -I(:,1).*W(:,2), -I(:,2).*W(:,2), I.^2, I, ones(size(I,1),1)];
else
A = [I, ones(size(I,1),1), -I(:,1).*W(:,1), -I(:,2).*W(:,1), zeros(size(I,1),3);
zeros(size(I,1),3), -I(:,1).*W(:,2), -I(:,2).*W(:,2), I, ones(size(I,1),1)];
end
Z = [W(:,1); W(:,2)];
B = (A’*A)^-1*A’*Z;
% function to map image coords to world coords
if order == 2
rectify = @(I) [(B(1)*I(:,1).^2 + B(2)*I(:,2).^2 + B(3)*I(:,1) + B(4)*I(:,2) + B(5))./ …
(B(6)*I(:,1).^2 + B(7)*I(:,2).^2 + B(8)*I(:,1) + B(9)*I(:,2) + 1), …
(B(10)*I(:,1).^2 + B(11)*I(:,2).^2 + B(12)*I(:,1) + B(13)*I(:,2) + B(14))./ …
(B(6)*I(:,1).^2 + B(7)*I(:,2).^2 + B(8)*I(:,1) + B(9)*I(:,2) + 1)];
else
rectify = @(I) [(B(1)*I(:,1) + B(2)*I(:,2) + B(3))./ …
(B(4)*I(:,1) + B(5)*I(:,2) + 1), …
(B(6)*I(:,1) + B(7)*I(:,2) + B(8))./ …
(B(4)*I(:,1) + B(5)*I(:,2) + 1)];
end
end
And here is an figure as reference. I want to rectify the raw image (convert it to real world coordinates) and plot it like in subplot(121)I have calibration images from four cameras in a 1×4 array. For simplicity, I’m showing the processing for just one camera. The image shows a calibration plate that contains black dots at a know separation.
The image is in pixels. I already post processes the image and obtain a rectification mapping function image coordinates to world coordinates. I input the location of known location in the image in image coordinates and the coordinates of these points in the real world. The output is the function that can take any point in image coordinates and convert it to real world coordinates.
I can plot the points in real world coordinates using the ‘scatter function’ (as in the figure below) but I’m having problems rectifying the whole image. I have no idea how to reshape the image with the new real world coordinates.
Here’s the data and code (data was a bit over 5MB).
clear
clc
close all
% I = points in image coordinates
% W = same points as in I but in real world coordinates
% calImg = Raw image in image coordinates
load("calImg.mat")
rectify_quad = cell(length(calImg),1); imagePoints2 = rectify_quad;
% CALIBRATION: compute the image rectification function for this camera
% from the world coords and image coords of the calibration dots (using
% a quadratic transformation: order = 2)
trans_order = 2;
rectify_quad{1} = calibrate_camera(I,W,trans_order);
imagePoints2{1} = rectify_quad{1}(I);
figure(1)
subplot(121)
imagesc(calImg); hold on
colormap(gray)
scatter(I(:,1), I(:,2), ‘r’, ‘LineWidth’,1)
axis equal
xlabel(‘pixel’)
ylabel(‘pixel’)
title(‘Image and detected points (Image coord)’)
subplot(122)
% I want to plot the rectified image in world coordinates similar to subplot (121)
scatter(imagePoints2{1}(:,1),imagePoints2{1}(:,2), ‘r’, ‘LineWidth’,1);
axis equal
xlabel(‘meters’)
ylabel(‘meters’)
title(‘detected points (real world coord)’)
function rectify = calibrate_camera(I,W,order)
% calculate the transformation function to convert image coordinates to
% world (physical) coordinates, including calibration (converting pixels to
% meters), undistortion, and rectification
%
% rectify: function handle to map image coordinates to world coordinates
% I: set of known calibration points in image coordinates (n x 2 vector) [px]
% W: set of known calibration points in world coordinates (n x 2 vector) [m]
% order: 1 for linear transformation (corrects for camera viewing angle but not lens distortion),
% 2 for quadratic transformation (corrects for camera viewing angle and lens distortion)
%
% references: Fujita et al 1998 (Water Res.), Creutin et al 2003 (J. Hydrol.)
%
% to use function handle: points_m = rectify(points_px)
% points_px: set of points in image coordinates (m x 2 vector) [px]
% points_m: set of points converted to world coordinates (m x 2 vector) [px]
% find transformation coefficients
if order == 2
A = [I.^2, I, ones(size(I,1),1), -I(:,1).^2.*W(:,1), -I(:,2).^2.*W(:,1), -I(:,1).*W(:,1), -I(:,2).*W(:,1), zeros(size(I,1),5);
zeros(size(I,1),5), -I(:,1).^2.*W(:,2), -I(:,2).^2.*W(:,2), -I(:,1).*W(:,2), -I(:,2).*W(:,2), I.^2, I, ones(size(I,1),1)];
else
A = [I, ones(size(I,1),1), -I(:,1).*W(:,1), -I(:,2).*W(:,1), zeros(size(I,1),3);
zeros(size(I,1),3), -I(:,1).*W(:,2), -I(:,2).*W(:,2), I, ones(size(I,1),1)];
end
Z = [W(:,1); W(:,2)];
B = (A’*A)^-1*A’*Z;
% function to map image coords to world coords
if order == 2
rectify = @(I) [(B(1)*I(:,1).^2 + B(2)*I(:,2).^2 + B(3)*I(:,1) + B(4)*I(:,2) + B(5))./ …
(B(6)*I(:,1).^2 + B(7)*I(:,2).^2 + B(8)*I(:,1) + B(9)*I(:,2) + 1), …
(B(10)*I(:,1).^2 + B(11)*I(:,2).^2 + B(12)*I(:,1) + B(13)*I(:,2) + B(14))./ …
(B(6)*I(:,1).^2 + B(7)*I(:,2).^2 + B(8)*I(:,1) + B(9)*I(:,2) + 1)];
else
rectify = @(I) [(B(1)*I(:,1) + B(2)*I(:,2) + B(3))./ …
(B(4)*I(:,1) + B(5)*I(:,2) + 1), …
(B(6)*I(:,1) + B(7)*I(:,2) + B(8))./ …
(B(4)*I(:,1) + B(5)*I(:,2) + 1)];
end
end
And here is an figure as reference. I want to rectify the raw image (convert it to real world coordinates) and plot it like in subplot(121) I have calibration images from four cameras in a 1×4 array. For simplicity, I’m showing the processing for just one camera. The image shows a calibration plate that contains black dots at a know separation.
The image is in pixels. I already post processes the image and obtain a rectification mapping function image coordinates to world coordinates. I input the location of known location in the image in image coordinates and the coordinates of these points in the real world. The output is the function that can take any point in image coordinates and convert it to real world coordinates.
I can plot the points in real world coordinates using the ‘scatter function’ (as in the figure below) but I’m having problems rectifying the whole image. I have no idea how to reshape the image with the new real world coordinates.
Here’s the data and code (data was a bit over 5MB).
clear
clc
close all
% I = points in image coordinates
% W = same points as in I but in real world coordinates
% calImg = Raw image in image coordinates
load("calImg.mat")
rectify_quad = cell(length(calImg),1); imagePoints2 = rectify_quad;
% CALIBRATION: compute the image rectification function for this camera
% from the world coords and image coords of the calibration dots (using
% a quadratic transformation: order = 2)
trans_order = 2;
rectify_quad{1} = calibrate_camera(I,W,trans_order);
imagePoints2{1} = rectify_quad{1}(I);
figure(1)
subplot(121)
imagesc(calImg); hold on
colormap(gray)
scatter(I(:,1), I(:,2), ‘r’, ‘LineWidth’,1)
axis equal
xlabel(‘pixel’)
ylabel(‘pixel’)
title(‘Image and detected points (Image coord)’)
subplot(122)
% I want to plot the rectified image in world coordinates similar to subplot (121)
scatter(imagePoints2{1}(:,1),imagePoints2{1}(:,2), ‘r’, ‘LineWidth’,1);
axis equal
xlabel(‘meters’)
ylabel(‘meters’)
title(‘detected points (real world coord)’)
function rectify = calibrate_camera(I,W,order)
% calculate the transformation function to convert image coordinates to
% world (physical) coordinates, including calibration (converting pixels to
% meters), undistortion, and rectification
%
% rectify: function handle to map image coordinates to world coordinates
% I: set of known calibration points in image coordinates (n x 2 vector) [px]
% W: set of known calibration points in world coordinates (n x 2 vector) [m]
% order: 1 for linear transformation (corrects for camera viewing angle but not lens distortion),
% 2 for quadratic transformation (corrects for camera viewing angle and lens distortion)
%
% references: Fujita et al 1998 (Water Res.), Creutin et al 2003 (J. Hydrol.)
%
% to use function handle: points_m = rectify(points_px)
% points_px: set of points in image coordinates (m x 2 vector) [px]
% points_m: set of points converted to world coordinates (m x 2 vector) [px]
% find transformation coefficients
if order == 2
A = [I.^2, I, ones(size(I,1),1), -I(:,1).^2.*W(:,1), -I(:,2).^2.*W(:,1), -I(:,1).*W(:,1), -I(:,2).*W(:,1), zeros(size(I,1),5);
zeros(size(I,1),5), -I(:,1).^2.*W(:,2), -I(:,2).^2.*W(:,2), -I(:,1).*W(:,2), -I(:,2).*W(:,2), I.^2, I, ones(size(I,1),1)];
else
A = [I, ones(size(I,1),1), -I(:,1).*W(:,1), -I(:,2).*W(:,1), zeros(size(I,1),3);
zeros(size(I,1),3), -I(:,1).*W(:,2), -I(:,2).*W(:,2), I, ones(size(I,1),1)];
end
Z = [W(:,1); W(:,2)];
B = (A’*A)^-1*A’*Z;
% function to map image coords to world coords
if order == 2
rectify = @(I) [(B(1)*I(:,1).^2 + B(2)*I(:,2).^2 + B(3)*I(:,1) + B(4)*I(:,2) + B(5))./ …
(B(6)*I(:,1).^2 + B(7)*I(:,2).^2 + B(8)*I(:,1) + B(9)*I(:,2) + 1), …
(B(10)*I(:,1).^2 + B(11)*I(:,2).^2 + B(12)*I(:,1) + B(13)*I(:,2) + B(14))./ …
(B(6)*I(:,1).^2 + B(7)*I(:,2).^2 + B(8)*I(:,1) + B(9)*I(:,2) + 1)];
else
rectify = @(I) [(B(1)*I(:,1) + B(2)*I(:,2) + B(3))./ …
(B(4)*I(:,1) + B(5)*I(:,2) + 1), …
(B(6)*I(:,1) + B(7)*I(:,2) + B(8))./ …
(B(4)*I(:,1) + B(5)*I(:,2) + 1)];
end
end
And here is an figure as reference. I want to rectify the raw image (convert it to real world coordinates) and plot it like in subplot(121) image processing, image analysis, orthorectification MATLAB Answers — New Questions
如何优化这段代码
clear,clc,close all
n=80; AB=400; M=100000;
prob=optimproblem;
x=optimvar(‘x’,3,n,’LowerBound’,zeros);
flagx=optimvar(‘flagx’,1,n,’Type’,’integer’,’LowerBound’,0,’UpperBound’,1);
con7=sum(x(3,:)./cos(x(1,:)))<=200; %未考虑EF遮蔽光线
con2=[];
for i=1:n
con2=[con2;x(1,i)<=pi/2;x(2,i)<=200;x(3,i)/cos(x(1,i))<=2.5];
end
con8=[];
for i=1:n-1
con8=[con8;sum(x(3,1:i))==x(2,i+1)];
end
p=100;
k=1./tan(2*x(1,:));
b=(x(2,:)+x(3,:)./cos(x(1,:)))./tan(2*x(1,:))+x(3,:).*tan(x(1,:));
b1=x(2,:)./tan(2*x(1,:));
[g, theta]=reflect_point(k,b,x,x(2,:),n);
[g1, theta1]=reflect_point(k,b1,x,x(2,:),n);
con4=[]; con5=[]; con6=[];
for i=1:n
con4=[con4;g(i)>=-5*flagx(i)+M*(flagx(i)-1)];
con5=[con5;g1(i)<=5*flagx(i)-M*(flagx(i)-1)];
con6=[con6;x(1,i)>=1/M];
end
con1=theta>=pi/2-x(1,:)*2;
con3=theta1>=pi/2-x(1,:)*2;
prob.Constraints.con1=con1;
prob.Constraints.con2=con2;
prob.Constraints.con3=con3;
prob.Constraints.con4=con4;
prob.Constraints.con5=con5;
prob.Constraints.con6=con6;
prob.Constraints.con7=con7;
prob.Constraints.con8=con8;
prob.Objective=-sum(flagx);
A(1,1:n)=pi/2;
A(2:3,:)=ones;
x00.x=A;
x00.flagx=ones(1,n);
[sol, fval, flag, out] = solve(prob,x00);
x=sol.x;
k=1./tan(2*x(1,:));
bb(1,:)=(x(2,:)+x(3,:)./cos(x(1,:)))./tan(2*x(1,:))+x(3,:).*sin(x(1,:));
bb(2,:)=x(2,:)./tan(2*x(1,:));
x01=p*k-sqrt(2*p.*bb(1,:)-200*p+(p*k).^2);
y01=(x01.^2)./(2*p)+100;
[gg(1,:), thetaa(1,:)]=reflect_point(k,bb(1,:),x,x(2,:),n);
[gg(2,:), thetaa(2,:)]=reflect_point(k,bb(2,:),x,x(2,:),n);
thetaa(3,:)=pi/2-x(1,:)*2;
function [g, theta] = reflect_point(k,b,x,d,n)
p=100;
x0=p*k-sqrt(2*p.*b-200*p+(p*k).^2);
y0=(x0.^2)./(2*p)+100;
theta=atan(-p./x0);
g=tan(2*theta+2*x(1,:)).*y0+x0;
endclear,clc,close all
n=80; AB=400; M=100000;
prob=optimproblem;
x=optimvar(‘x’,3,n,’LowerBound’,zeros);
flagx=optimvar(‘flagx’,1,n,’Type’,’integer’,’LowerBound’,0,’UpperBound’,1);
con7=sum(x(3,:)./cos(x(1,:)))<=200; %未考虑EF遮蔽光线
con2=[];
for i=1:n
con2=[con2;x(1,i)<=pi/2;x(2,i)<=200;x(3,i)/cos(x(1,i))<=2.5];
end
con8=[];
for i=1:n-1
con8=[con8;sum(x(3,1:i))==x(2,i+1)];
end
p=100;
k=1./tan(2*x(1,:));
b=(x(2,:)+x(3,:)./cos(x(1,:)))./tan(2*x(1,:))+x(3,:).*tan(x(1,:));
b1=x(2,:)./tan(2*x(1,:));
[g, theta]=reflect_point(k,b,x,x(2,:),n);
[g1, theta1]=reflect_point(k,b1,x,x(2,:),n);
con4=[]; con5=[]; con6=[];
for i=1:n
con4=[con4;g(i)>=-5*flagx(i)+M*(flagx(i)-1)];
con5=[con5;g1(i)<=5*flagx(i)-M*(flagx(i)-1)];
con6=[con6;x(1,i)>=1/M];
end
con1=theta>=pi/2-x(1,:)*2;
con3=theta1>=pi/2-x(1,:)*2;
prob.Constraints.con1=con1;
prob.Constraints.con2=con2;
prob.Constraints.con3=con3;
prob.Constraints.con4=con4;
prob.Constraints.con5=con5;
prob.Constraints.con6=con6;
prob.Constraints.con7=con7;
prob.Constraints.con8=con8;
prob.Objective=-sum(flagx);
A(1,1:n)=pi/2;
A(2:3,:)=ones;
x00.x=A;
x00.flagx=ones(1,n);
[sol, fval, flag, out] = solve(prob,x00);
x=sol.x;
k=1./tan(2*x(1,:));
bb(1,:)=(x(2,:)+x(3,:)./cos(x(1,:)))./tan(2*x(1,:))+x(3,:).*sin(x(1,:));
bb(2,:)=x(2,:)./tan(2*x(1,:));
x01=p*k-sqrt(2*p.*bb(1,:)-200*p+(p*k).^2);
y01=(x01.^2)./(2*p)+100;
[gg(1,:), thetaa(1,:)]=reflect_point(k,bb(1,:),x,x(2,:),n);
[gg(2,:), thetaa(2,:)]=reflect_point(k,bb(2,:),x,x(2,:),n);
thetaa(3,:)=pi/2-x(1,:)*2;
function [g, theta] = reflect_point(k,b,x,d,n)
p=100;
x0=p*k-sqrt(2*p.*b-200*p+(p*k).^2);
y0=(x0.^2)./(2*p)+100;
theta=atan(-p./x0);
g=tan(2*theta+2*x(1,:)).*y0+x0;
end clear,clc,close all
n=80; AB=400; M=100000;
prob=optimproblem;
x=optimvar(‘x’,3,n,’LowerBound’,zeros);
flagx=optimvar(‘flagx’,1,n,’Type’,’integer’,’LowerBound’,0,’UpperBound’,1);
con7=sum(x(3,:)./cos(x(1,:)))<=200; %未考虑EF遮蔽光线
con2=[];
for i=1:n
con2=[con2;x(1,i)<=pi/2;x(2,i)<=200;x(3,i)/cos(x(1,i))<=2.5];
end
con8=[];
for i=1:n-1
con8=[con8;sum(x(3,1:i))==x(2,i+1)];
end
p=100;
k=1./tan(2*x(1,:));
b=(x(2,:)+x(3,:)./cos(x(1,:)))./tan(2*x(1,:))+x(3,:).*tan(x(1,:));
b1=x(2,:)./tan(2*x(1,:));
[g, theta]=reflect_point(k,b,x,x(2,:),n);
[g1, theta1]=reflect_point(k,b1,x,x(2,:),n);
con4=[]; con5=[]; con6=[];
for i=1:n
con4=[con4;g(i)>=-5*flagx(i)+M*(flagx(i)-1)];
con5=[con5;g1(i)<=5*flagx(i)-M*(flagx(i)-1)];
con6=[con6;x(1,i)>=1/M];
end
con1=theta>=pi/2-x(1,:)*2;
con3=theta1>=pi/2-x(1,:)*2;
prob.Constraints.con1=con1;
prob.Constraints.con2=con2;
prob.Constraints.con3=con3;
prob.Constraints.con4=con4;
prob.Constraints.con5=con5;
prob.Constraints.con6=con6;
prob.Constraints.con7=con7;
prob.Constraints.con8=con8;
prob.Objective=-sum(flagx);
A(1,1:n)=pi/2;
A(2:3,:)=ones;
x00.x=A;
x00.flagx=ones(1,n);
[sol, fval, flag, out] = solve(prob,x00);
x=sol.x;
k=1./tan(2*x(1,:));
bb(1,:)=(x(2,:)+x(3,:)./cos(x(1,:)))./tan(2*x(1,:))+x(3,:).*sin(x(1,:));
bb(2,:)=x(2,:)./tan(2*x(1,:));
x01=p*k-sqrt(2*p.*bb(1,:)-200*p+(p*k).^2);
y01=(x01.^2)./(2*p)+100;
[gg(1,:), thetaa(1,:)]=reflect_point(k,bb(1,:),x,x(2,:),n);
[gg(2,:), thetaa(2,:)]=reflect_point(k,bb(2,:),x,x(2,:),n);
thetaa(3,:)=pi/2-x(1,:)*2;
function [g, theta] = reflect_point(k,b,x,d,n)
p=100;
x0=p*k-sqrt(2*p.*b-200*p+(p*k).^2);
y0=(x0.^2)./(2*p)+100;
theta=atan(-p./x0);
g=tan(2*theta+2*x(1,:)).*y0+x0;
end 优化问题 MATLAB Answers — New Questions
fsolve error: Unable to perform assignment because the indices on the left side are not compatible with the size of the right side.
I’m tring to run the following set of code:
%% SW Model: Primary VR
dfreq_SW_vr = -6e-3;
% MMS
gamma_SW = 0.0001*pi : 0.0001 : 0.415*pi;
x0_SW = [0, 0];
options = optimoptions(@fsolve,’StepTolerance’,1e-15);
for ct = 1:numel(gamma_SW)
x_SW(ct,:) = fsolve( @(x_SW)bicycle_SW(x_SW, gamma_SW(ct)), x0_SW, options);
x0_SW = [ x_SW(end,1)+0.001 , x_SW(end,2)+0.003]; % Update IC
end
figure(8)
% MMS (Stable)
plot(x_SW(1:10,1), 2*x_SW(1:10,2),’Color’,[.7 .7 .7],’LineWidth’,2.5)
hold on; grid on;
%% Function File (SW VR)
function Eqn_SW = bicycle_SW(x_SW, gamma_SW)
global w b g1 g2 g3 g4 dfreq_SW_vr nu f1b f2b zet_0_0 eta_0_0 zet_1_0 eta_1_0 zet_2_0 eta_2_0 zet_3_0 eta_3_0
% Note: Eqn(1) = Real, Eqn(2) = Imag
% Note: x_–(1) = d, x_–(2) = a
Eqn_SW(1) = (2.*x_SW(2).*dfreq_SW_vr.*w(1).*g2) + ((1./4).*x_SW(1).*nu).*(f1b.*zet_0_0 + f2b.*eta_0_0).*g1.*cos(gamma_SW) + ((1./4).*x_SW(1).*nu).*(f1b.*zet_1_0 + f2b.*eta_1_0).*x_SW(2).*g2 + ((3./16).*x_SW(1).*nu).*(f1b.*zet_2_0 + f2b.*eta_2_0).*(x_SW(2).^2).*g3.*cos(gamma_SW) + ((3./16).*x_SW(1).*nu).*(f1b.*zet_3_0 + f2b.*eta_3_0).*(x_SW(2).^3).*g4;
Eqn_SW(2) = (-1./2).*b.*w(1).*x_SW(2).*g2 + ((1./4).*x_SW(1).*nu).*(f1b.*zet_0_0 + f2b.*eta_0_0).*g1.*sin(gamma_SW) + ((1./16).*x_SW(1).*nu).*(f1b.*zet_2_0 + f2b.*eta_2_0).*(x_SW(2).^2).*g3.*sin(gamma_SW);
end
But I get the following error when I run it:
Unable to perform assignment because the indices on the left side are not compatible with the size of the right side.
Error in untitled3>bicycle_SW (line 91)
Eqn_SW(1) = (2.*x_SW(2).*dfreq_SW_vr.*w(1).*g2) + ((1./4).*x_SW(1).*nu).*(f1b.*zet_0_0 +
f2b.*eta_0_0).*g1.*cos(gamma_SW) + ((1./4).*x_SW(1).*nu).*(f1b.*zet_1_0 + f2b.*eta_1_0).*x_SW(2).*g2 +
((3./16).*x_SW(1).*nu).*(f1b.*zet_2_0 + f2b.*eta_2_0).*(x_SW(2).^2).*g3.*cos(gamma_SW) +
((3./16).*x_SW(1).*nu).*(f1b.*zet_3_0 + f2b.*eta_3_0).*(x_SW(2).^3).*g4;
Error in untitled3>@(x_SW)bicycle_SW(x_SW,gamma_SW(ct)) (line 13)
x_SW(ct,:) = fsolve( @(x_SW)bicycle_SW(x_SW, gamma_SW(ct)), x0_SW, options);
Not sure what I’m doing wrong… Any help would be much appreciated.I’m tring to run the following set of code:
%% SW Model: Primary VR
dfreq_SW_vr = -6e-3;
% MMS
gamma_SW = 0.0001*pi : 0.0001 : 0.415*pi;
x0_SW = [0, 0];
options = optimoptions(@fsolve,’StepTolerance’,1e-15);
for ct = 1:numel(gamma_SW)
x_SW(ct,:) = fsolve( @(x_SW)bicycle_SW(x_SW, gamma_SW(ct)), x0_SW, options);
x0_SW = [ x_SW(end,1)+0.001 , x_SW(end,2)+0.003]; % Update IC
end
figure(8)
% MMS (Stable)
plot(x_SW(1:10,1), 2*x_SW(1:10,2),’Color’,[.7 .7 .7],’LineWidth’,2.5)
hold on; grid on;
%% Function File (SW VR)
function Eqn_SW = bicycle_SW(x_SW, gamma_SW)
global w b g1 g2 g3 g4 dfreq_SW_vr nu f1b f2b zet_0_0 eta_0_0 zet_1_0 eta_1_0 zet_2_0 eta_2_0 zet_3_0 eta_3_0
% Note: Eqn(1) = Real, Eqn(2) = Imag
% Note: x_–(1) = d, x_–(2) = a
Eqn_SW(1) = (2.*x_SW(2).*dfreq_SW_vr.*w(1).*g2) + ((1./4).*x_SW(1).*nu).*(f1b.*zet_0_0 + f2b.*eta_0_0).*g1.*cos(gamma_SW) + ((1./4).*x_SW(1).*nu).*(f1b.*zet_1_0 + f2b.*eta_1_0).*x_SW(2).*g2 + ((3./16).*x_SW(1).*nu).*(f1b.*zet_2_0 + f2b.*eta_2_0).*(x_SW(2).^2).*g3.*cos(gamma_SW) + ((3./16).*x_SW(1).*nu).*(f1b.*zet_3_0 + f2b.*eta_3_0).*(x_SW(2).^3).*g4;
Eqn_SW(2) = (-1./2).*b.*w(1).*x_SW(2).*g2 + ((1./4).*x_SW(1).*nu).*(f1b.*zet_0_0 + f2b.*eta_0_0).*g1.*sin(gamma_SW) + ((1./16).*x_SW(1).*nu).*(f1b.*zet_2_0 + f2b.*eta_2_0).*(x_SW(2).^2).*g3.*sin(gamma_SW);
end
But I get the following error when I run it:
Unable to perform assignment because the indices on the left side are not compatible with the size of the right side.
Error in untitled3>bicycle_SW (line 91)
Eqn_SW(1) = (2.*x_SW(2).*dfreq_SW_vr.*w(1).*g2) + ((1./4).*x_SW(1).*nu).*(f1b.*zet_0_0 +
f2b.*eta_0_0).*g1.*cos(gamma_SW) + ((1./4).*x_SW(1).*nu).*(f1b.*zet_1_0 + f2b.*eta_1_0).*x_SW(2).*g2 +
((3./16).*x_SW(1).*nu).*(f1b.*zet_2_0 + f2b.*eta_2_0).*(x_SW(2).^2).*g3.*cos(gamma_SW) +
((3./16).*x_SW(1).*nu).*(f1b.*zet_3_0 + f2b.*eta_3_0).*(x_SW(2).^3).*g4;
Error in untitled3>@(x_SW)bicycle_SW(x_SW,gamma_SW(ct)) (line 13)
x_SW(ct,:) = fsolve( @(x_SW)bicycle_SW(x_SW, gamma_SW(ct)), x0_SW, options);
Not sure what I’m doing wrong… Any help would be much appreciated. I’m tring to run the following set of code:
%% SW Model: Primary VR
dfreq_SW_vr = -6e-3;
% MMS
gamma_SW = 0.0001*pi : 0.0001 : 0.415*pi;
x0_SW = [0, 0];
options = optimoptions(@fsolve,’StepTolerance’,1e-15);
for ct = 1:numel(gamma_SW)
x_SW(ct,:) = fsolve( @(x_SW)bicycle_SW(x_SW, gamma_SW(ct)), x0_SW, options);
x0_SW = [ x_SW(end,1)+0.001 , x_SW(end,2)+0.003]; % Update IC
end
figure(8)
% MMS (Stable)
plot(x_SW(1:10,1), 2*x_SW(1:10,2),’Color’,[.7 .7 .7],’LineWidth’,2.5)
hold on; grid on;
%% Function File (SW VR)
function Eqn_SW = bicycle_SW(x_SW, gamma_SW)
global w b g1 g2 g3 g4 dfreq_SW_vr nu f1b f2b zet_0_0 eta_0_0 zet_1_0 eta_1_0 zet_2_0 eta_2_0 zet_3_0 eta_3_0
% Note: Eqn(1) = Real, Eqn(2) = Imag
% Note: x_–(1) = d, x_–(2) = a
Eqn_SW(1) = (2.*x_SW(2).*dfreq_SW_vr.*w(1).*g2) + ((1./4).*x_SW(1).*nu).*(f1b.*zet_0_0 + f2b.*eta_0_0).*g1.*cos(gamma_SW) + ((1./4).*x_SW(1).*nu).*(f1b.*zet_1_0 + f2b.*eta_1_0).*x_SW(2).*g2 + ((3./16).*x_SW(1).*nu).*(f1b.*zet_2_0 + f2b.*eta_2_0).*(x_SW(2).^2).*g3.*cos(gamma_SW) + ((3./16).*x_SW(1).*nu).*(f1b.*zet_3_0 + f2b.*eta_3_0).*(x_SW(2).^3).*g4;
Eqn_SW(2) = (-1./2).*b.*w(1).*x_SW(2).*g2 + ((1./4).*x_SW(1).*nu).*(f1b.*zet_0_0 + f2b.*eta_0_0).*g1.*sin(gamma_SW) + ((1./16).*x_SW(1).*nu).*(f1b.*zet_2_0 + f2b.*eta_2_0).*(x_SW(2).^2).*g3.*sin(gamma_SW);
end
But I get the following error when I run it:
Unable to perform assignment because the indices on the left side are not compatible with the size of the right side.
Error in untitled3>bicycle_SW (line 91)
Eqn_SW(1) = (2.*x_SW(2).*dfreq_SW_vr.*w(1).*g2) + ((1./4).*x_SW(1).*nu).*(f1b.*zet_0_0 +
f2b.*eta_0_0).*g1.*cos(gamma_SW) + ((1./4).*x_SW(1).*nu).*(f1b.*zet_1_0 + f2b.*eta_1_0).*x_SW(2).*g2 +
((3./16).*x_SW(1).*nu).*(f1b.*zet_2_0 + f2b.*eta_2_0).*(x_SW(2).^2).*g3.*cos(gamma_SW) +
((3./16).*x_SW(1).*nu).*(f1b.*zet_3_0 + f2b.*eta_3_0).*(x_SW(2).^3).*g4;
Error in untitled3>@(x_SW)bicycle_SW(x_SW,gamma_SW(ct)) (line 13)
x_SW(ct,:) = fsolve( @(x_SW)bicycle_SW(x_SW, gamma_SW(ct)), x0_SW, options);
Not sure what I’m doing wrong… Any help would be much appreciated. fsolve, compatible, indices MATLAB Answers — New Questions