Category: Matlab
Category Archives: Matlab
Function with for loop doesn’t always work
I’ve written a function that writes a C structure into a header file using fprintf(). It seems to not work at all when the input array is small and works partially when the input array is big.
function deg = path2header(fid, deg, comment, option)
% Print path into a header file
% The angle can be set as ‘rad’, ‘deg’, or ‘rev’ in the option.
% fid – File indentity
% deg – [deg, vmax, amax, dwell]
% comment – Name of the path
% option – ‘rad’, ‘deg’, or ‘rev’
if strcmp(option,’rad’)
deg = deg./(2*pi);
elseif strcmp(option,’deg’)
deg = deg./360;
elseif strcmp(option,’rev’)
else
disp("Error option in path2header()")
return
end
[nseg,m]=size(deg);
disp(nseg)
fprintf(fid,’//—%sn’, comment);
fprintf(fid,’//—%sn’, datestr(now,0));
fprintf(fid,’ char headerTime[] = "%s";n’,datestr(now,0));
fprintf(fid,’ int nseg = %d; // number of pointsn’,nseg);
fprintf(fid,’ segtmySegs[%d]={ // define the array of points in revsn’,nseg);
for i = 1:nseg-1
fprintf(fid,’ {‘);
for j = [1,2,3]
fprintf(fid,’%e, ‘,deg(i,j));
end
fprintf(fid,’%e},n’,deg(i,4));
end
fprintf(fid,’ {‘);
for j = [1,2,3]
fprintf(fid,’%e, ‘,deg(nseg,j));
end
fprintf(fid,’%e},n’,deg(nseg,4));
fprintf(fid,’}n };n’);
This is an example of the test code that results in an empty testPath.h file.
clear;
deg = [ones(3,3), zeros(3,1)];
HeaderFileName = "testPath.h";
fid = fopen(HeaderFileName,’W’);
comment = "matlab sramp";
path2header(fid, deg, comment, ‘rev’)
The function is part of a bigger script to convert angles from an inverse kinematics equation to be saved as C header file to preprogram a motor’s path thus the n x 4 matrix.
Please help. Thx!I’ve written a function that writes a C structure into a header file using fprintf(). It seems to not work at all when the input array is small and works partially when the input array is big.
function deg = path2header(fid, deg, comment, option)
% Print path into a header file
% The angle can be set as ‘rad’, ‘deg’, or ‘rev’ in the option.
% fid – File indentity
% deg – [deg, vmax, amax, dwell]
% comment – Name of the path
% option – ‘rad’, ‘deg’, or ‘rev’
if strcmp(option,’rad’)
deg = deg./(2*pi);
elseif strcmp(option,’deg’)
deg = deg./360;
elseif strcmp(option,’rev’)
else
disp("Error option in path2header()")
return
end
[nseg,m]=size(deg);
disp(nseg)
fprintf(fid,’//—%sn’, comment);
fprintf(fid,’//—%sn’, datestr(now,0));
fprintf(fid,’ char headerTime[] = "%s";n’,datestr(now,0));
fprintf(fid,’ int nseg = %d; // number of pointsn’,nseg);
fprintf(fid,’ segtmySegs[%d]={ // define the array of points in revsn’,nseg);
for i = 1:nseg-1
fprintf(fid,’ {‘);
for j = [1,2,3]
fprintf(fid,’%e, ‘,deg(i,j));
end
fprintf(fid,’%e},n’,deg(i,4));
end
fprintf(fid,’ {‘);
for j = [1,2,3]
fprintf(fid,’%e, ‘,deg(nseg,j));
end
fprintf(fid,’%e},n’,deg(nseg,4));
fprintf(fid,’}n };n’);
This is an example of the test code that results in an empty testPath.h file.
clear;
deg = [ones(3,3), zeros(3,1)];
HeaderFileName = "testPath.h";
fid = fopen(HeaderFileName,’W’);
comment = "matlab sramp";
path2header(fid, deg, comment, ‘rev’)
The function is part of a bigger script to convert angles from an inverse kinematics equation to be saved as C header file to preprogram a motor’s path thus the n x 4 matrix.
Please help. Thx! I’ve written a function that writes a C structure into a header file using fprintf(). It seems to not work at all when the input array is small and works partially when the input array is big.
function deg = path2header(fid, deg, comment, option)
% Print path into a header file
% The angle can be set as ‘rad’, ‘deg’, or ‘rev’ in the option.
% fid – File indentity
% deg – [deg, vmax, amax, dwell]
% comment – Name of the path
% option – ‘rad’, ‘deg’, or ‘rev’
if strcmp(option,’rad’)
deg = deg./(2*pi);
elseif strcmp(option,’deg’)
deg = deg./360;
elseif strcmp(option,’rev’)
else
disp("Error option in path2header()")
return
end
[nseg,m]=size(deg);
disp(nseg)
fprintf(fid,’//—%sn’, comment);
fprintf(fid,’//—%sn’, datestr(now,0));
fprintf(fid,’ char headerTime[] = "%s";n’,datestr(now,0));
fprintf(fid,’ int nseg = %d; // number of pointsn’,nseg);
fprintf(fid,’ segtmySegs[%d]={ // define the array of points in revsn’,nseg);
for i = 1:nseg-1
fprintf(fid,’ {‘);
for j = [1,2,3]
fprintf(fid,’%e, ‘,deg(i,j));
end
fprintf(fid,’%e},n’,deg(i,4));
end
fprintf(fid,’ {‘);
for j = [1,2,3]
fprintf(fid,’%e, ‘,deg(nseg,j));
end
fprintf(fid,’%e},n’,deg(nseg,4));
fprintf(fid,’}n };n’);
This is an example of the test code that results in an empty testPath.h file.
clear;
deg = [ones(3,3), zeros(3,1)];
HeaderFileName = "testPath.h";
fid = fopen(HeaderFileName,’W’);
comment = "matlab sramp";
path2header(fid, deg, comment, ‘rev’)
The function is part of a bigger script to convert angles from an inverse kinematics equation to be saved as C header file to preprogram a motor’s path thus the n x 4 matrix.
Please help. Thx! for loop, function MATLAB Answers — New Questions
Why do I get an error in bateman_Radon220 example file?
I am trying to understend the following examplefile (bateman_Radon220.m). But when I run run code I get an error:
Warning: Error updating FunctionLine.
DataSpace or ColorSpace transform method failed.
The problem is that the A_sum does not plot entirely right, so I guess it has something to do with that?!
Kind regard Brian
% Rn-220 (Thoron) decay chain
%
% For the basics have a look at bateman.m.
%
%
% (r41=64%)-l4-> Po-212
% / l5
% Rn-220 -l1-> Po-216 -l2-> Pb-212 -l3-> Bi-212 —-> Pb-208 (stable)
% /l6
% (r42=36%)-l4-> Tl-208
% 1: Rn-220 t(1/2) = 55.6 s
% 2: Po-216 t(1/2) = 0.15 s
% 3: Pb-212 t(1/2) = 10.6 h
% 4: Bi-212 t(1/2) = 60.6 min
% 5: Po-212 t(1/2) = 299 ns
% 6: Tl-208 t(1/2) = 3.05 min
%
% l = lamda = decay constant
% t(1/2) = half-life
% N0 is the initial quantity of substance that will decay.
% N(t) is the quantity that still remains and has not yet decayed after a time t.
% dN/dt = -l*N
% N(t) = N0*exp(-l*t)
% l = log(2)/t(1/2) (log means the natural logarithm)
% A = activity; is the number of decays per unit time of a radioactive sample
% A = l*N
% A0 = l1*N0
% rxy = branching ratio
% et = elution time
clear all;
syms N1(t) N2(t) N3(t) N4(t) N5(t) N6(t) l1 l2 l3 l4 l5 l6 r41 r42 N0 A0;
eq_1 = diff(N1(t),t) == -l1*N1(t); % decay of Rn-220
eq_2 = diff(N2(t),t) == -l2*N2(t)+l1*N1(t); % decay of Po-216 and formation from Rn-220
eq_3 = diff(N3(t),t) == -l3*N3(t)+l2*N2(t); % decay of Pb-212 and formation from Po-216
eq_4 = diff(N4(t),t) == -l4*N4(t)+l3*N3(t); % decay of Bi-212 and formation from Pb-212
eq_5 = diff(N5(t),t) == -l5*N5(t)+r41*l4*N4(t); % decay of Po-212 and formation from Bi-212
eq_6 = diff(N6(t),t) == -l6*N6(t)+r42*l4*N4(t); % decay of Tl-208 and formation from Bi-212
% solve the system of diff. equations
% conditions N1(t=0) = N0; N2 to N5(t=0) = 0
sol = dsolve ([eq_1 eq_2 eq_3 eq_4 eq_5 eq_6, N1(0)==N0 N2(0)==0 N3(0)==0 N4(0)==0 N5(0)==0 N6(0)==0]);
A1 = sol.N1*l1; % transform N to A
A2 = sol.N2*l2; % transform N to A
A3 = sol.N3*l3; % transform N to A
A4 = sol.N4*l4; % transform N to A
A5 = sol.N5*l5; % transform N to A
A6 = sol.N6*l6; % transform N to A
A1 = subs(A1,N0*l1,A0); % substitute N0*l1 with A0
A2 = subs(A2,N0*l1,A0); % substitute N0*l1 with A0
A3 = subs(A3,N0*l1,A0); % substitute N0*l1 with A0
A4 = subs(A4,N0*l1,A0); % substitute N0*l1 with A0
A5 = subs(A5,N0*l1,A0); % substitute N0*l1 with A0
A6 = subs(A6,N0*l1,A0); % substitute N0*l1 with A0
A1 = A1 / A0; % divide by A0 to get relative activities
A2 = A2 / A0; % divide by A0 to get relative activities
A3 = A3 / A0; % divide by A0 to get relative activities
A4 = A4 / A0; % divide by A0 to get relative activities
A5 = A5 / A0; % divide by A0 to get relative activities
A6 = A6 / A0; % divide by A0 to get relative activities
A2 = simplify(A2,’Steps’,40); % simplify
A3 = simplify(A3,’Steps’,40); % simplify
A4 = simplify(A4,’Steps’,40); % simplify
A5 = simplify(A5,’Steps’,40); % simplify
A6 = simplify(A6,’Steps’,40); % simplify
% ———————————————————————–
% values to change
l1_v = log(2)/55.6; % l for Rn-220 /s; l=log(2)/t(1/2)
l2_v = log(2)/0.15; % l for Po-216 /s; l=log(2)/t(1/2)
l3_v = log(2)/(10.6*60*60); % l for Pb-212 /s; l=log(2)/t(1/2)
l4_v = log(2)/(60.6*60); % l for Bi-212 /s; l=log(2)/t(1/2)
l5_v = log(2)/2.99e-7; % l for Po-212 /s; l=log(2)/t(1/2)
l6_v = log(2)/(3.05*60); % l for Tl-208 /s; l=log(2)/t(1/2)
r41_v = 0.64; % Bi-212 branching to Po-212
r42_v = 0.36; % Bi-212 branching to Tl-208
% ———————————————————————–
% symbolic substitution of constants with "real" values
A1s = subs(A1, [l1,l2,l3,l4,l5,l6,r41,r42], [l1_v,l2_v,l3_v,l4_v,l5_v,l6_v,r41_v,r42_v]);
A2s = subs(A2, [l1,l2,l3,l4,l5,l6,r41,r42], [l1_v,l2_v,l3_v,l4_v,l5_v,l6_v,r41_v,r42_v]);
A3s = subs(A3, [l1,l2,l3,l4,l5,l6,r41,r42], [l1_v,l2_v,l3_v,l4_v,l5_v,l6_v,r41_v,r42_v]);
A4s = subs(A4, [l1,l2,l3,l4,l5,l6,r41,r42], [l1_v,l2_v,l3_v,l4_v,l5_v,l6_v,r41_v,r42_v]);
A5s = subs(A5, [l1,l2,l3,l4,l5,l6,r41,r42], [l1_v,l2_v,l3_v,l4_v,l5_v,l6_v,r41_v,r42_v]);
A6s = subs(A6, [l1,l2,l3,l4,l5,l6,r41,r42], [l1_v,l2_v,l3_v,l4_v,l5_v,l6_v,r41_v,r42_v]);
A_sum = A1s + A2s + A3s + A4s + A5s + A6s; % sum of all activities
% plot section
hold on;
fplot(A1s,’Color’,’blue’);
fplot(A2s,’Color’,’red’);
fplot(A3s,’Color’,’green’);
fplot(A4s,’Color’,’yellow’);
fplot(A5s,’Color’,’magenta’);
fplot(A6s,’Color’,’black’);
fplot(A_sum,’Color’,’black’,’LineStyle’,’–‘);
hold off
% plot options
ax=gca;
ax.Title.String = {‘Decay of Rn-220’};
ax.YLim=[1e-15 3];
ax.XLim=[1e-4 5e6];
ax.YScale=’log’;
ax.XScale=’log’;
ax.XLabel.String=’t /s’;
ax.YLabel.String=’rel. activity A/A_0′;
grid on;
grid minor;
legend(‘Rn-220′,’Po-216′,’Pb-212′,’Bi-212′,’Po-212′,’Tl-208′,’A_{sum}’,’Location’,’best’);I am trying to understend the following examplefile (bateman_Radon220.m). But when I run run code I get an error:
Warning: Error updating FunctionLine.
DataSpace or ColorSpace transform method failed.
The problem is that the A_sum does not plot entirely right, so I guess it has something to do with that?!
Kind regard Brian
% Rn-220 (Thoron) decay chain
%
% For the basics have a look at bateman.m.
%
%
% (r41=64%)-l4-> Po-212
% / l5
% Rn-220 -l1-> Po-216 -l2-> Pb-212 -l3-> Bi-212 —-> Pb-208 (stable)
% /l6
% (r42=36%)-l4-> Tl-208
% 1: Rn-220 t(1/2) = 55.6 s
% 2: Po-216 t(1/2) = 0.15 s
% 3: Pb-212 t(1/2) = 10.6 h
% 4: Bi-212 t(1/2) = 60.6 min
% 5: Po-212 t(1/2) = 299 ns
% 6: Tl-208 t(1/2) = 3.05 min
%
% l = lamda = decay constant
% t(1/2) = half-life
% N0 is the initial quantity of substance that will decay.
% N(t) is the quantity that still remains and has not yet decayed after a time t.
% dN/dt = -l*N
% N(t) = N0*exp(-l*t)
% l = log(2)/t(1/2) (log means the natural logarithm)
% A = activity; is the number of decays per unit time of a radioactive sample
% A = l*N
% A0 = l1*N0
% rxy = branching ratio
% et = elution time
clear all;
syms N1(t) N2(t) N3(t) N4(t) N5(t) N6(t) l1 l2 l3 l4 l5 l6 r41 r42 N0 A0;
eq_1 = diff(N1(t),t) == -l1*N1(t); % decay of Rn-220
eq_2 = diff(N2(t),t) == -l2*N2(t)+l1*N1(t); % decay of Po-216 and formation from Rn-220
eq_3 = diff(N3(t),t) == -l3*N3(t)+l2*N2(t); % decay of Pb-212 and formation from Po-216
eq_4 = diff(N4(t),t) == -l4*N4(t)+l3*N3(t); % decay of Bi-212 and formation from Pb-212
eq_5 = diff(N5(t),t) == -l5*N5(t)+r41*l4*N4(t); % decay of Po-212 and formation from Bi-212
eq_6 = diff(N6(t),t) == -l6*N6(t)+r42*l4*N4(t); % decay of Tl-208 and formation from Bi-212
% solve the system of diff. equations
% conditions N1(t=0) = N0; N2 to N5(t=0) = 0
sol = dsolve ([eq_1 eq_2 eq_3 eq_4 eq_5 eq_6, N1(0)==N0 N2(0)==0 N3(0)==0 N4(0)==0 N5(0)==0 N6(0)==0]);
A1 = sol.N1*l1; % transform N to A
A2 = sol.N2*l2; % transform N to A
A3 = sol.N3*l3; % transform N to A
A4 = sol.N4*l4; % transform N to A
A5 = sol.N5*l5; % transform N to A
A6 = sol.N6*l6; % transform N to A
A1 = subs(A1,N0*l1,A0); % substitute N0*l1 with A0
A2 = subs(A2,N0*l1,A0); % substitute N0*l1 with A0
A3 = subs(A3,N0*l1,A0); % substitute N0*l1 with A0
A4 = subs(A4,N0*l1,A0); % substitute N0*l1 with A0
A5 = subs(A5,N0*l1,A0); % substitute N0*l1 with A0
A6 = subs(A6,N0*l1,A0); % substitute N0*l1 with A0
A1 = A1 / A0; % divide by A0 to get relative activities
A2 = A2 / A0; % divide by A0 to get relative activities
A3 = A3 / A0; % divide by A0 to get relative activities
A4 = A4 / A0; % divide by A0 to get relative activities
A5 = A5 / A0; % divide by A0 to get relative activities
A6 = A6 / A0; % divide by A0 to get relative activities
A2 = simplify(A2,’Steps’,40); % simplify
A3 = simplify(A3,’Steps’,40); % simplify
A4 = simplify(A4,’Steps’,40); % simplify
A5 = simplify(A5,’Steps’,40); % simplify
A6 = simplify(A6,’Steps’,40); % simplify
% ———————————————————————–
% values to change
l1_v = log(2)/55.6; % l for Rn-220 /s; l=log(2)/t(1/2)
l2_v = log(2)/0.15; % l for Po-216 /s; l=log(2)/t(1/2)
l3_v = log(2)/(10.6*60*60); % l for Pb-212 /s; l=log(2)/t(1/2)
l4_v = log(2)/(60.6*60); % l for Bi-212 /s; l=log(2)/t(1/2)
l5_v = log(2)/2.99e-7; % l for Po-212 /s; l=log(2)/t(1/2)
l6_v = log(2)/(3.05*60); % l for Tl-208 /s; l=log(2)/t(1/2)
r41_v = 0.64; % Bi-212 branching to Po-212
r42_v = 0.36; % Bi-212 branching to Tl-208
% ———————————————————————–
% symbolic substitution of constants with "real" values
A1s = subs(A1, [l1,l2,l3,l4,l5,l6,r41,r42], [l1_v,l2_v,l3_v,l4_v,l5_v,l6_v,r41_v,r42_v]);
A2s = subs(A2, [l1,l2,l3,l4,l5,l6,r41,r42], [l1_v,l2_v,l3_v,l4_v,l5_v,l6_v,r41_v,r42_v]);
A3s = subs(A3, [l1,l2,l3,l4,l5,l6,r41,r42], [l1_v,l2_v,l3_v,l4_v,l5_v,l6_v,r41_v,r42_v]);
A4s = subs(A4, [l1,l2,l3,l4,l5,l6,r41,r42], [l1_v,l2_v,l3_v,l4_v,l5_v,l6_v,r41_v,r42_v]);
A5s = subs(A5, [l1,l2,l3,l4,l5,l6,r41,r42], [l1_v,l2_v,l3_v,l4_v,l5_v,l6_v,r41_v,r42_v]);
A6s = subs(A6, [l1,l2,l3,l4,l5,l6,r41,r42], [l1_v,l2_v,l3_v,l4_v,l5_v,l6_v,r41_v,r42_v]);
A_sum = A1s + A2s + A3s + A4s + A5s + A6s; % sum of all activities
% plot section
hold on;
fplot(A1s,’Color’,’blue’);
fplot(A2s,’Color’,’red’);
fplot(A3s,’Color’,’green’);
fplot(A4s,’Color’,’yellow’);
fplot(A5s,’Color’,’magenta’);
fplot(A6s,’Color’,’black’);
fplot(A_sum,’Color’,’black’,’LineStyle’,’–‘);
hold off
% plot options
ax=gca;
ax.Title.String = {‘Decay of Rn-220’};
ax.YLim=[1e-15 3];
ax.XLim=[1e-4 5e6];
ax.YScale=’log’;
ax.XScale=’log’;
ax.XLabel.String=’t /s’;
ax.YLabel.String=’rel. activity A/A_0′;
grid on;
grid minor;
legend(‘Rn-220′,’Po-216′,’Pb-212′,’Bi-212′,’Po-212′,’Tl-208′,’A_{sum}’,’Location’,’best’); I am trying to understend the following examplefile (bateman_Radon220.m). But when I run run code I get an error:
Warning: Error updating FunctionLine.
DataSpace or ColorSpace transform method failed.
The problem is that the A_sum does not plot entirely right, so I guess it has something to do with that?!
Kind regard Brian
% Rn-220 (Thoron) decay chain
%
% For the basics have a look at bateman.m.
%
%
% (r41=64%)-l4-> Po-212
% / l5
% Rn-220 -l1-> Po-216 -l2-> Pb-212 -l3-> Bi-212 —-> Pb-208 (stable)
% /l6
% (r42=36%)-l4-> Tl-208
% 1: Rn-220 t(1/2) = 55.6 s
% 2: Po-216 t(1/2) = 0.15 s
% 3: Pb-212 t(1/2) = 10.6 h
% 4: Bi-212 t(1/2) = 60.6 min
% 5: Po-212 t(1/2) = 299 ns
% 6: Tl-208 t(1/2) = 3.05 min
%
% l = lamda = decay constant
% t(1/2) = half-life
% N0 is the initial quantity of substance that will decay.
% N(t) is the quantity that still remains and has not yet decayed after a time t.
% dN/dt = -l*N
% N(t) = N0*exp(-l*t)
% l = log(2)/t(1/2) (log means the natural logarithm)
% A = activity; is the number of decays per unit time of a radioactive sample
% A = l*N
% A0 = l1*N0
% rxy = branching ratio
% et = elution time
clear all;
syms N1(t) N2(t) N3(t) N4(t) N5(t) N6(t) l1 l2 l3 l4 l5 l6 r41 r42 N0 A0;
eq_1 = diff(N1(t),t) == -l1*N1(t); % decay of Rn-220
eq_2 = diff(N2(t),t) == -l2*N2(t)+l1*N1(t); % decay of Po-216 and formation from Rn-220
eq_3 = diff(N3(t),t) == -l3*N3(t)+l2*N2(t); % decay of Pb-212 and formation from Po-216
eq_4 = diff(N4(t),t) == -l4*N4(t)+l3*N3(t); % decay of Bi-212 and formation from Pb-212
eq_5 = diff(N5(t),t) == -l5*N5(t)+r41*l4*N4(t); % decay of Po-212 and formation from Bi-212
eq_6 = diff(N6(t),t) == -l6*N6(t)+r42*l4*N4(t); % decay of Tl-208 and formation from Bi-212
% solve the system of diff. equations
% conditions N1(t=0) = N0; N2 to N5(t=0) = 0
sol = dsolve ([eq_1 eq_2 eq_3 eq_4 eq_5 eq_6, N1(0)==N0 N2(0)==0 N3(0)==0 N4(0)==0 N5(0)==0 N6(0)==0]);
A1 = sol.N1*l1; % transform N to A
A2 = sol.N2*l2; % transform N to A
A3 = sol.N3*l3; % transform N to A
A4 = sol.N4*l4; % transform N to A
A5 = sol.N5*l5; % transform N to A
A6 = sol.N6*l6; % transform N to A
A1 = subs(A1,N0*l1,A0); % substitute N0*l1 with A0
A2 = subs(A2,N0*l1,A0); % substitute N0*l1 with A0
A3 = subs(A3,N0*l1,A0); % substitute N0*l1 with A0
A4 = subs(A4,N0*l1,A0); % substitute N0*l1 with A0
A5 = subs(A5,N0*l1,A0); % substitute N0*l1 with A0
A6 = subs(A6,N0*l1,A0); % substitute N0*l1 with A0
A1 = A1 / A0; % divide by A0 to get relative activities
A2 = A2 / A0; % divide by A0 to get relative activities
A3 = A3 / A0; % divide by A0 to get relative activities
A4 = A4 / A0; % divide by A0 to get relative activities
A5 = A5 / A0; % divide by A0 to get relative activities
A6 = A6 / A0; % divide by A0 to get relative activities
A2 = simplify(A2,’Steps’,40); % simplify
A3 = simplify(A3,’Steps’,40); % simplify
A4 = simplify(A4,’Steps’,40); % simplify
A5 = simplify(A5,’Steps’,40); % simplify
A6 = simplify(A6,’Steps’,40); % simplify
% ———————————————————————–
% values to change
l1_v = log(2)/55.6; % l for Rn-220 /s; l=log(2)/t(1/2)
l2_v = log(2)/0.15; % l for Po-216 /s; l=log(2)/t(1/2)
l3_v = log(2)/(10.6*60*60); % l for Pb-212 /s; l=log(2)/t(1/2)
l4_v = log(2)/(60.6*60); % l for Bi-212 /s; l=log(2)/t(1/2)
l5_v = log(2)/2.99e-7; % l for Po-212 /s; l=log(2)/t(1/2)
l6_v = log(2)/(3.05*60); % l for Tl-208 /s; l=log(2)/t(1/2)
r41_v = 0.64; % Bi-212 branching to Po-212
r42_v = 0.36; % Bi-212 branching to Tl-208
% ———————————————————————–
% symbolic substitution of constants with "real" values
A1s = subs(A1, [l1,l2,l3,l4,l5,l6,r41,r42], [l1_v,l2_v,l3_v,l4_v,l5_v,l6_v,r41_v,r42_v]);
A2s = subs(A2, [l1,l2,l3,l4,l5,l6,r41,r42], [l1_v,l2_v,l3_v,l4_v,l5_v,l6_v,r41_v,r42_v]);
A3s = subs(A3, [l1,l2,l3,l4,l5,l6,r41,r42], [l1_v,l2_v,l3_v,l4_v,l5_v,l6_v,r41_v,r42_v]);
A4s = subs(A4, [l1,l2,l3,l4,l5,l6,r41,r42], [l1_v,l2_v,l3_v,l4_v,l5_v,l6_v,r41_v,r42_v]);
A5s = subs(A5, [l1,l2,l3,l4,l5,l6,r41,r42], [l1_v,l2_v,l3_v,l4_v,l5_v,l6_v,r41_v,r42_v]);
A6s = subs(A6, [l1,l2,l3,l4,l5,l6,r41,r42], [l1_v,l2_v,l3_v,l4_v,l5_v,l6_v,r41_v,r42_v]);
A_sum = A1s + A2s + A3s + A4s + A5s + A6s; % sum of all activities
% plot section
hold on;
fplot(A1s,’Color’,’blue’);
fplot(A2s,’Color’,’red’);
fplot(A3s,’Color’,’green’);
fplot(A4s,’Color’,’yellow’);
fplot(A5s,’Color’,’magenta’);
fplot(A6s,’Color’,’black’);
fplot(A_sum,’Color’,’black’,’LineStyle’,’–‘);
hold off
% plot options
ax=gca;
ax.Title.String = {‘Decay of Rn-220’};
ax.YLim=[1e-15 3];
ax.XLim=[1e-4 5e6];
ax.YScale=’log’;
ax.XScale=’log’;
ax.XLabel.String=’t /s’;
ax.YLabel.String=’rel. activity A/A_0′;
grid on;
grid minor;
legend(‘Rn-220′,’Po-216′,’Pb-212′,’Bi-212′,’Po-212′,’Tl-208′,’A_{sum}’,’Location’,’best’); bateman, decay, radioactivity, radon220, examplefile MATLAB Answers — New Questions
Unable to open the requested feature. Check your internet connection and proxy settings in MATLAB Web preferences and then try starting the feature again. Detailed information: Error code: -6 Error message: ERR_FILE_NOT_FOUND
i can’t set up iti can’t set up it i can’t set up it set up MATLAB Answers — New Questions
I required MATLAB Code that Calculate SNR of two Devices communicating Via IRS, Including Channel gains, phase shift value etc
Two Devices are cpmmunicating Via IRS. !st Channel Gain is From Source to IRS, 2nd Channel Gain is From IRS to Destination, IRS Having 32 Reflecting Elements and Apply Phase Shift. Plot SNR w.r.t No of IRS Eleemnts. How SINR is changingTwo Devices are cpmmunicating Via IRS. !st Channel Gain is From Source to IRS, 2nd Channel Gain is From IRS to Destination, IRS Having 32 Reflecting Elements and Apply Phase Shift. Plot SNR w.r.t No of IRS Eleemnts. How SINR is changing Two Devices are cpmmunicating Via IRS. !st Channel Gain is From Source to IRS, 2nd Channel Gain is From IRS to Destination, IRS Having 32 Reflecting Elements and Apply Phase Shift. Plot SNR w.r.t No of IRS Eleemnts. How SINR is changing irs, snr MATLAB Answers — New Questions
Hysteesis Design tool can’t display graphics– -Powergui Error
Hi,
I’m trying to use powergui Hysteesis Design tool,but it can’t display graphics.Report “matlab.graphics. internal. figfile.Figfile/read> (hobiect, eventdata)power hysteresis DisplayPushButton Callback , hob ject,eventdata,guidata(hobject)“
Does anyone have information on this error?
Thanks!Hi,
I’m trying to use powergui Hysteesis Design tool,but it can’t display graphics.Report “matlab.graphics. internal. figfile.Figfile/read> (hobiect, eventdata)power hysteresis DisplayPushButton Callback , hob ject,eventdata,guidata(hobject)“
Does anyone have information on this error?
Thanks! Hi,
I’m trying to use powergui Hysteesis Design tool,but it can’t display graphics.Report “matlab.graphics. internal. figfile.Figfile/read> (hobiect, eventdata)power hysteresis DisplayPushButton Callback , hob ject,eventdata,guidata(hobject)“
Does anyone have information on this error?
Thanks! hysteesis design tool MATLAB Answers — New Questions
Running Feather RP2040 with Simulink
I’m working on a project which requires me to take data from sensors connected to a Feather RP2040 and simulate it live on Simulink. I can’t seem to figure out how to read I2C from the Feather to MatLab. All the add-ons I could find were for Arduinos or RP hardware other than the RP2040. Is there anyway to get around that? All I need is to get the data from the Feather to MatLab in the moment.I’m working on a project which requires me to take data from sensors connected to a Feather RP2040 and simulate it live on Simulink. I can’t seem to figure out how to read I2C from the Feather to MatLab. All the add-ons I could find were for Arduinos or RP hardware other than the RP2040. Is there anyway to get around that? All I need is to get the data from the Feather to MatLab in the moment. I’m working on a project which requires me to take data from sensors connected to a Feather RP2040 and simulate it live on Simulink. I can’t seem to figure out how to read I2C from the Feather to MatLab. All the add-ons I could find were for Arduinos or RP hardware other than the RP2040. Is there anyway to get around that? All I need is to get the data from the Feather to MatLab in the moment. feather, rp2040, simulink, matlab MATLAB Answers — New Questions
wait for several system calls to finish
I am calling three external programs and can only continue in the matlab program after all programs are finished. Each of them work independently of each other and can start at the same time
[staus1,~] = system(‘cmd1 &’);
[staus2,~] = system(‘cmd2 &’);
[staus3,~] = system(‘cmd3 &’);
% how to check if all of them are finished?
Using ampersand & symbol, I reached that they start nearly at the same time, but how can I check when they are all finished? In my case, the returned status variables can be zero (successful) or negative (not successful). Hence, checking status for any value does not help, I guess.I am calling three external programs and can only continue in the matlab program after all programs are finished. Each of them work independently of each other and can start at the same time
[staus1,~] = system(‘cmd1 &’);
[staus2,~] = system(‘cmd2 &’);
[staus3,~] = system(‘cmd3 &’);
% how to check if all of them are finished?
Using ampersand & symbol, I reached that they start nearly at the same time, but how can I check when they are all finished? In my case, the returned status variables can be zero (successful) or negative (not successful). Hence, checking status for any value does not help, I guess. I am calling three external programs and can only continue in the matlab program after all programs are finished. Each of them work independently of each other and can start at the same time
[staus1,~] = system(‘cmd1 &’);
[staus2,~] = system(‘cmd2 &’);
[staus3,~] = system(‘cmd3 &’);
% how to check if all of them are finished?
Using ampersand & symbol, I reached that they start nearly at the same time, but how can I check when they are all finished? In my case, the returned status variables can be zero (successful) or negative (not successful). Hence, checking status for any value does not help, I guess. linux, system MATLAB Answers — New Questions
Dot product does not work for symbolic vectors
Hi, I’m working on dynamics simulation with matlab, so I have the vector r with the size of [3 1]. I examine the calculation
dot(r,r) – (r(1)^2+r(2)^2 +r(3)^2) and I got an answer different than 0. Is there any explanation for this ?
syms t g real
syms alpha1(t) beta1(t) gamma1(t)
syms m1 m2 m3 m4 real
syms h d1 d2 r1 r2 r3 r4 t1 t2 t3 L real
syms a b c d5 real
r = [(a*b*c*d5*m1*sin(conj(gamma1(t))-gamma(t)))/(-a*b*c*m1 +pi*m3*r1^2*t1 +pi*m1*r1^2*t1);
-(0.5000*(- a*b*c^2*m1 + a*b*c*m1*t1 + 3.1416*m3*r1^2*t1*t3 + 3.1416*m3*r1^2*t1^2))/(- a*b*c*m1 + 3.1416*m3*r1^2*t1 + 3.1416*m1*r1^2*t1);
-(a*b*c*d5*m1*cos(conj(gamma1(t)) – gamma1(t)))/(- a*b*c*m1 + 3.1416*m3*r1^2*t1 + 3.1416*m1*r1^2*t1) ];
r_x = r(1) ;
r_y = r(2) ;
r_z = r(3) ;
% dot minus sum of elements. This should be 0
% I think this is a problem with dot function of MATLAB.
dot(r,r) – (r_x^2 + r_y^2 + r_z^2 )Hi, I’m working on dynamics simulation with matlab, so I have the vector r with the size of [3 1]. I examine the calculation
dot(r,r) – (r(1)^2+r(2)^2 +r(3)^2) and I got an answer different than 0. Is there any explanation for this ?
syms t g real
syms alpha1(t) beta1(t) gamma1(t)
syms m1 m2 m3 m4 real
syms h d1 d2 r1 r2 r3 r4 t1 t2 t3 L real
syms a b c d5 real
r = [(a*b*c*d5*m1*sin(conj(gamma1(t))-gamma(t)))/(-a*b*c*m1 +pi*m3*r1^2*t1 +pi*m1*r1^2*t1);
-(0.5000*(- a*b*c^2*m1 + a*b*c*m1*t1 + 3.1416*m3*r1^2*t1*t3 + 3.1416*m3*r1^2*t1^2))/(- a*b*c*m1 + 3.1416*m3*r1^2*t1 + 3.1416*m1*r1^2*t1);
-(a*b*c*d5*m1*cos(conj(gamma1(t)) – gamma1(t)))/(- a*b*c*m1 + 3.1416*m3*r1^2*t1 + 3.1416*m1*r1^2*t1) ];
r_x = r(1) ;
r_y = r(2) ;
r_z = r(3) ;
% dot minus sum of elements. This should be 0
% I think this is a problem with dot function of MATLAB.
dot(r,r) – (r_x^2 + r_y^2 + r_z^2 ) Hi, I’m working on dynamics simulation with matlab, so I have the vector r with the size of [3 1]. I examine the calculation
dot(r,r) – (r(1)^2+r(2)^2 +r(3)^2) and I got an answer different than 0. Is there any explanation for this ?
syms t g real
syms alpha1(t) beta1(t) gamma1(t)
syms m1 m2 m3 m4 real
syms h d1 d2 r1 r2 r3 r4 t1 t2 t3 L real
syms a b c d5 real
r = [(a*b*c*d5*m1*sin(conj(gamma1(t))-gamma(t)))/(-a*b*c*m1 +pi*m3*r1^2*t1 +pi*m1*r1^2*t1);
-(0.5000*(- a*b*c^2*m1 + a*b*c*m1*t1 + 3.1416*m3*r1^2*t1*t3 + 3.1416*m3*r1^2*t1^2))/(- a*b*c*m1 + 3.1416*m3*r1^2*t1 + 3.1416*m1*r1^2*t1);
-(a*b*c*d5*m1*cos(conj(gamma1(t)) – gamma1(t)))/(- a*b*c*m1 + 3.1416*m3*r1^2*t1 + 3.1416*m1*r1^2*t1) ];
r_x = r(1) ;
r_y = r(2) ;
r_z = r(3) ;
% dot minus sum of elements. This should be 0
% I think this is a problem with dot function of MATLAB.
dot(r,r) – (r_x^2 + r_y^2 + r_z^2 ) dot, symbolics, dynamics, matrices MATLAB Answers — New Questions
how to resolve that error?
how to resolve that error?how to resolve that error? how to resolve that error? how to resolve that error? MATLAB Answers — New Questions
what is the plot in matlab and how to write a code for it?
Please write a code for me I do not know how to code in matlabPlease write a code for me I do not know how to code in matlab Please write a code for me I do not know how to code in matlab matlab MATLAB Answers — New Questions
what is image processing and how to write a code for it?
I donot know how to write a code for image processing please write a code for meI donot know how to write a code for image processing please write a code for me I donot know how to write a code for image processing please write a code for me matlab MATLAB Answers — New Questions
How to get values from a csv every 5 minutes in simulink
Hello,
i am using a matlab script with a timer to read every 5 minutes a value from a csv table. Now i need this values in Simulink. How can i achieve that? Or what approach would you suggest to get every 5 minutes a value from a csv table in Simulink?Hello,
i am using a matlab script with a timer to read every 5 minutes a value from a csv table. Now i need this values in Simulink. How can i achieve that? Or what approach would you suggest to get every 5 minutes a value from a csv table in Simulink? Hello,
i am using a matlab script with a timer to read every 5 minutes a value from a csv table. Now i need this values in Simulink. How can i achieve that? Or what approach would you suggest to get every 5 minutes a value from a csv table in Simulink? csv, simulink MATLAB Answers — New Questions
Thingspeak – how to reboot a LORA node if it stops sending data?
I have a LORA node up-linking data to a Thingspeak channel via The Things Network (TTN). Sometimes it stops sending data and I reboot it by logging into my conmsole on TTN and sending a re-boot down-link message to the node. Is there any way I can transmit a downlink message to the node direct from Thinkspeak?
Thanks, SteveI have a LORA node up-linking data to a Thingspeak channel via The Things Network (TTN). Sometimes it stops sending data and I reboot it by logging into my conmsole on TTN and sending a re-boot down-link message to the node. Is there any way I can transmit a downlink message to the node direct from Thinkspeak?
Thanks, Steve I have a LORA node up-linking data to a Thingspeak channel via The Things Network (TTN). Sometimes it stops sending data and I reboot it by logging into my conmsole on TTN and sending a re-boot down-link message to the node. Is there any way I can transmit a downlink message to the node direct from Thinkspeak?
Thanks, Steve thingspeak, downlink, lora MATLAB Answers — New Questions
Error occurred while executing External Mode MEX-file ‘ext_comm’: Failed to connect to the target,
hi everyone.
when i’m upload my simulink model to c2000 F28379D its give error as follows,
Build process completed successfully
Output port 2 of ‘C2000codePLL2017b_v2/Demux3’ is not connected. [4 similar]
Component:Simulink | Category:Block warning
Unconnected line found in ‘C2000codePLL2017b_v2’ (outside of current view)
Component:Simulink | Category:Block diagram warning
Error occurred while executing External Mode MEX-file ‘ext_comm’: Failed to connect to the targetError occurred while executing External Mode MEX-file ‘ext_comm’: Failed to connect to the target. A time-out occurred while waiting for the connection response from the target. Possible reasons for the time-out:
a) The target is not switched on.
b) The target is not connected to your host machine.
c) The application for the model is not running on the target. You might have clicked the Stop button. If the Run button is not dimmed, click it. Otherwise, click the Build button, which downloads and runs your application on the target.
please help me to solve this error.
Thank you in advancehi everyone.
when i’m upload my simulink model to c2000 F28379D its give error as follows,
Build process completed successfully
Output port 2 of ‘C2000codePLL2017b_v2/Demux3’ is not connected. [4 similar]
Component:Simulink | Category:Block warning
Unconnected line found in ‘C2000codePLL2017b_v2’ (outside of current view)
Component:Simulink | Category:Block diagram warning
Error occurred while executing External Mode MEX-file ‘ext_comm’: Failed to connect to the targetError occurred while executing External Mode MEX-file ‘ext_comm’: Failed to connect to the target. A time-out occurred while waiting for the connection response from the target. Possible reasons for the time-out:
a) The target is not switched on.
b) The target is not connected to your host machine.
c) The application for the model is not running on the target. You might have clicked the Stop button. If the Run button is not dimmed, click it. Otherwise, click the Build button, which downloads and runs your application on the target.
please help me to solve this error.
Thank you in advance hi everyone.
when i’m upload my simulink model to c2000 F28379D its give error as follows,
Build process completed successfully
Output port 2 of ‘C2000codePLL2017b_v2/Demux3’ is not connected. [4 similar]
Component:Simulink | Category:Block warning
Unconnected line found in ‘C2000codePLL2017b_v2’ (outside of current view)
Component:Simulink | Category:Block diagram warning
Error occurred while executing External Mode MEX-file ‘ext_comm’: Failed to connect to the targetError occurred while executing External Mode MEX-file ‘ext_comm’: Failed to connect to the target. A time-out occurred while waiting for the connection response from the target. Possible reasons for the time-out:
a) The target is not switched on.
b) The target is not connected to your host machine.
c) The application for the model is not running on the target. You might have clicked the Stop button. If the Run button is not dimmed, click it. Otherwise, click the Build button, which downloads and runs your application on the target.
please help me to solve this error.
Thank you in advance mathlab simulink, texas instruments, c2000, delfino f28379d MATLAB Answers — New Questions
Simulate single precision arithmetic in Matlab?
Hi! I’m trying to replicate, in Matlab, compiled algorithms implemented in C using single-precision arithmetic; I have the C source code, and an executable which is reputed to implement said C source, but no convenient means to compile and link said source myself. (Yes, I know I can download the Microsoft C compiler, incl. a no-frills version of Visual Studio, for free, but that’s hardly "convenient.") I’ve implemented the algorithms and I’m 99% sure they’re right because the "residuals" (the difference between my results and those of the original C implementations) are more-or-less randomly distributed around zero, and about 85% of them are within half of the precision of the C implementations’ output (which varies, but, for example, when it’s 1e-4, 85% of my residuals satisfy abs(res)<5e-5). It’s the other 15% that leave me 1% uncertain, and my attempts to explain the discrepancy have brought me to the single/double precision issue: I have good cause to suspect that the C implementations are using single-precision arithmetic, and I’ve been led to believe that Matlab always performs double-precision arithmetic, even on single precision "data," yes? (I tried making ‘single’ all the data in my implementation, and confirmed after each "gross" calculation that the output remained so, but that only reduced the number of bad residuals by 3 out of 4118 total input points.) So, I’m wondering if there’s either a "convenient" way to simulate single-precision arithmetic in Matlab (I searched File Exchange for relevant functions, but found none) or to estimate/bound the "error" introduced by using single-precision arithmetic (including power/root and exponential/log functions) instead of double. Thanks!Hi! I’m trying to replicate, in Matlab, compiled algorithms implemented in C using single-precision arithmetic; I have the C source code, and an executable which is reputed to implement said C source, but no convenient means to compile and link said source myself. (Yes, I know I can download the Microsoft C compiler, incl. a no-frills version of Visual Studio, for free, but that’s hardly "convenient.") I’ve implemented the algorithms and I’m 99% sure they’re right because the "residuals" (the difference between my results and those of the original C implementations) are more-or-less randomly distributed around zero, and about 85% of them are within half of the precision of the C implementations’ output (which varies, but, for example, when it’s 1e-4, 85% of my residuals satisfy abs(res)<5e-5). It’s the other 15% that leave me 1% uncertain, and my attempts to explain the discrepancy have brought me to the single/double precision issue: I have good cause to suspect that the C implementations are using single-precision arithmetic, and I’ve been led to believe that Matlab always performs double-precision arithmetic, even on single precision "data," yes? (I tried making ‘single’ all the data in my implementation, and confirmed after each "gross" calculation that the output remained so, but that only reduced the number of bad residuals by 3 out of 4118 total input points.) So, I’m wondering if there’s either a "convenient" way to simulate single-precision arithmetic in Matlab (I searched File Exchange for relevant functions, but found none) or to estimate/bound the "error" introduced by using single-precision arithmetic (including power/root and exponential/log functions) instead of double. Thanks! Hi! I’m trying to replicate, in Matlab, compiled algorithms implemented in C using single-precision arithmetic; I have the C source code, and an executable which is reputed to implement said C source, but no convenient means to compile and link said source myself. (Yes, I know I can download the Microsoft C compiler, incl. a no-frills version of Visual Studio, for free, but that’s hardly "convenient.") I’ve implemented the algorithms and I’m 99% sure they’re right because the "residuals" (the difference between my results and those of the original C implementations) are more-or-less randomly distributed around zero, and about 85% of them are within half of the precision of the C implementations’ output (which varies, but, for example, when it’s 1e-4, 85% of my residuals satisfy abs(res)<5e-5). It’s the other 15% that leave me 1% uncertain, and my attempts to explain the discrepancy have brought me to the single/double precision issue: I have good cause to suspect that the C implementations are using single-precision arithmetic, and I’ve been led to believe that Matlab always performs double-precision arithmetic, even on single precision "data," yes? (I tried making ‘single’ all the data in my implementation, and confirmed after each "gross" calculation that the output remained so, but that only reduced the number of bad residuals by 3 out of 4118 total input points.) So, I’m wondering if there’s either a "convenient" way to simulate single-precision arithmetic in Matlab (I searched File Exchange for relevant functions, but found none) or to estimate/bound the "error" introduced by using single-precision arithmetic (including power/root and exponential/log functions) instead of double. Thanks! single precision arithmetic, estimating error MATLAB Answers — New Questions
Data fiting in Matlab
Y pluss = (y*Ut)/mu (1)
Upluss = U/Ut (2)
Ypluss=Upluss + exp (-k*B) *(exp(k*Upluss) -1 -k*Upluss + ((k*Upluss).^2)./2 -((k*Upluss).^3)./6 (3)
I have these threee eqautions. in these eqautions, Value of y, U, B, mu and K are known.
How can i find the value of Ut by usind data fiting of the 3rd equation?Y pluss = (y*Ut)/mu (1)
Upluss = U/Ut (2)
Ypluss=Upluss + exp (-k*B) *(exp(k*Upluss) -1 -k*Upluss + ((k*Upluss).^2)./2 -((k*Upluss).^3)./6 (3)
I have these threee eqautions. in these eqautions, Value of y, U, B, mu and K are known.
How can i find the value of Ut by usind data fiting of the 3rd equation? Y pluss = (y*Ut)/mu (1)
Upluss = U/Ut (2)
Ypluss=Upluss + exp (-k*B) *(exp(k*Upluss) -1 -k*Upluss + ((k*Upluss).^2)./2 -((k*Upluss).^3)./6 (3)
I have these threee eqautions. in these eqautions, Value of y, U, B, mu and K are known.
How can i find the value of Ut by usind data fiting of the 3rd equation? matlab data fit MATLAB Answers — New Questions
how to find area and volume from a .stl file?
I need to find the area and volume of 3D triangulation. I used [F,V] = stlBinaryRead(‘DWB1.stl’); to read the file "DWB1.stl". But when I used stlBinaryRead.m to get volume, it gives NaN. Hot to fix this? I used V'( all( isnan( V’ ), 2 ), : )) = (); to remove NaN. but did not work.I need to find the area and volume of 3D triangulation. I used [F,V] = stlBinaryRead(‘DWB1.stl’); to read the file "DWB1.stl". But when I used stlBinaryRead.m to get volume, it gives NaN. Hot to fix this? I used V'( all( isnan( V’ ), 2 ), : )) = (); to remove NaN. but did not work. I need to find the area and volume of 3D triangulation. I used [F,V] = stlBinaryRead(‘DWB1.stl’); to read the file "DWB1.stl". But when I used stlBinaryRead.m to get volume, it gives NaN. Hot to fix this? I used V'( all( isnan( V’ ), 2 ), : )) = (); to remove NaN. but did not work. stl file, 3d-triangulation, nan, volume MATLAB Answers — New Questions
How to complete Gaussian elimination?
Hello everyone, I am looking for an answer using Gaussian elimination, and the values diverge at the end. How do I fix it to get an accurate answer?
Here is my code.
clc; clear all; close all;
A = [2 0 1 ; -2 4 1 ;-1 -1 3];
b = [8 0 2]’;
sz = size(A,1);
disp ([A b]);
for i = 2 :1: sz
for j = 1:1:i-1
k = A(j,j)/A(i,j);
A(i,:) = k * A(i,:) – A(j,:);
b(i) = k * b(i) – b(j);
disp([A b]);
pause(3)
end
end
for i = sz-1:-1:1
for j = sz:-1:i+1
k = A(j,j)/A(i,j);
A(i,:) = k*A(i,:)-A(j,:);
b(i) = k* b(i) – b(j);
disp([A b]);
pause(3)
end
end
x = b./diag(A);
disp(x);Hello everyone, I am looking for an answer using Gaussian elimination, and the values diverge at the end. How do I fix it to get an accurate answer?
Here is my code.
clc; clear all; close all;
A = [2 0 1 ; -2 4 1 ;-1 -1 3];
b = [8 0 2]’;
sz = size(A,1);
disp ([A b]);
for i = 2 :1: sz
for j = 1:1:i-1
k = A(j,j)/A(i,j);
A(i,:) = k * A(i,:) – A(j,:);
b(i) = k * b(i) – b(j);
disp([A b]);
pause(3)
end
end
for i = sz-1:-1:1
for j = sz:-1:i+1
k = A(j,j)/A(i,j);
A(i,:) = k*A(i,:)-A(j,:);
b(i) = k* b(i) – b(j);
disp([A b]);
pause(3)
end
end
x = b./diag(A);
disp(x); Hello everyone, I am looking for an answer using Gaussian elimination, and the values diverge at the end. How do I fix it to get an accurate answer?
Here is my code.
clc; clear all; close all;
A = [2 0 1 ; -2 4 1 ;-1 -1 3];
b = [8 0 2]’;
sz = size(A,1);
disp ([A b]);
for i = 2 :1: sz
for j = 1:1:i-1
k = A(j,j)/A(i,j);
A(i,:) = k * A(i,:) – A(j,:);
b(i) = k * b(i) – b(j);
disp([A b]);
pause(3)
end
end
for i = sz-1:-1:1
for j = sz:-1:i+1
k = A(j,j)/A(i,j);
A(i,:) = k*A(i,:)-A(j,:);
b(i) = k* b(i) – b(j);
disp([A b]);
pause(3)
end
end
x = b./diag(A);
disp(x); gaussian elimination MATLAB Answers — New Questions
Cannot install addon – unexpected error
See version information below. I launched as admin, and as not admin. Nothing I could do would work. It would start download and at about a third each time it would fail.
Log file output:
—
(Apr 20, 2024 22:00:22) # Today’s Date:
(Apr 20, 2024 22:00:22) Apr 20, 2024 22:00:22 -0500
(Apr 20, 2024 22:00:22)
(Apr 20, 2024 22:00:22) Starting Add-On Installer
(Apr 20, 2024 22:00:22) System Info
(Apr 20, 2024 22:00:22) OS: Windows Version 10.0 (Build 22631)
(Apr 20, 2024 22:00:22) Arch: amd64
(Apr 20, 2024 22:00:22) Language: ja_JP
(Apr 20, 2024 22:00:22) Release Description: R2024a Update 1
(Apr 20, 2024 22:00:22) Installer’s Build Number: 24.1.0.2568132
(Apr 20, 2024 22:00:22) Session key: 52678390-7d09-4970-8dd5-77d4ef98a1b5
(Apr 20, 2024 22:00:23) Proxy host & port are not active
(Apr 20, 2024 22:00:23) Proxy username & password are not active
(Apr 20, 2024 22:00:23) Proxy validation is successful.
(Apr 20, 2024 22:00:23) C:Program FilesMATLABR2024a is writeable
(Apr 20, 2024 22:00:23) Write permissions validation is successful.
(Apr 20, 2024 22:00:23) Starting download of product meta data
(Apr 20, 2024 22:00:24) Finished downloading product meta data
(Apr 20, 2024 22:00:27) Product data validation is successful.
(Apr 20, 2024 22:00:27) MATLAB detection validation is successful.
(Apr 20, 2024 22:00:27) Install Root: C:Program FilesMATLABR2024a
(Apr 20, 2024 22:00:28) 1%
(Apr 20, 2024 22:00:28) 4%
(Apr 20, 2024 22:00:29) 5%
(Apr 20, 2024 22:00:29) 10%
(Apr 20, 2024 22:00:29) 13%
(Apr 20, 2024 22:00:29) 15%
(Apr 20, 2024 22:00:30) 17%
(Apr 20, 2024 22:00:31) 21%
(Apr 20, 2024 22:00:31) 24%
(Apr 20, 2024 22:00:32) 25%
(Apr 20, 2024 22:00:32) 27%
(Apr 20, 2024 22:00:32) 予期せぬ問題が発生しました。<br>インストーラーの再実行をお試しください。<br><br>ヘルプについては、こちらの <a HREF="https://www.mathworks.com/pi_suoe_uai_R2024a_win64">MATLAB Answer</a> を参照してください。<br>Package cannot be read or empty.
(Apr 20, 2024 22:00:38) Exiting with status -2
(Apr 20, 2024 22:00:38) End – UnsuccessfulSee version information below. I launched as admin, and as not admin. Nothing I could do would work. It would start download and at about a third each time it would fail.
Log file output:
—
(Apr 20, 2024 22:00:22) # Today’s Date:
(Apr 20, 2024 22:00:22) Apr 20, 2024 22:00:22 -0500
(Apr 20, 2024 22:00:22)
(Apr 20, 2024 22:00:22) Starting Add-On Installer
(Apr 20, 2024 22:00:22) System Info
(Apr 20, 2024 22:00:22) OS: Windows Version 10.0 (Build 22631)
(Apr 20, 2024 22:00:22) Arch: amd64
(Apr 20, 2024 22:00:22) Language: ja_JP
(Apr 20, 2024 22:00:22) Release Description: R2024a Update 1
(Apr 20, 2024 22:00:22) Installer’s Build Number: 24.1.0.2568132
(Apr 20, 2024 22:00:22) Session key: 52678390-7d09-4970-8dd5-77d4ef98a1b5
(Apr 20, 2024 22:00:23) Proxy host & port are not active
(Apr 20, 2024 22:00:23) Proxy username & password are not active
(Apr 20, 2024 22:00:23) Proxy validation is successful.
(Apr 20, 2024 22:00:23) C:Program FilesMATLABR2024a is writeable
(Apr 20, 2024 22:00:23) Write permissions validation is successful.
(Apr 20, 2024 22:00:23) Starting download of product meta data
(Apr 20, 2024 22:00:24) Finished downloading product meta data
(Apr 20, 2024 22:00:27) Product data validation is successful.
(Apr 20, 2024 22:00:27) MATLAB detection validation is successful.
(Apr 20, 2024 22:00:27) Install Root: C:Program FilesMATLABR2024a
(Apr 20, 2024 22:00:28) 1%
(Apr 20, 2024 22:00:28) 4%
(Apr 20, 2024 22:00:29) 5%
(Apr 20, 2024 22:00:29) 10%
(Apr 20, 2024 22:00:29) 13%
(Apr 20, 2024 22:00:29) 15%
(Apr 20, 2024 22:00:30) 17%
(Apr 20, 2024 22:00:31) 21%
(Apr 20, 2024 22:00:31) 24%
(Apr 20, 2024 22:00:32) 25%
(Apr 20, 2024 22:00:32) 27%
(Apr 20, 2024 22:00:32) 予期せぬ問題が発生しました。<br>インストーラーの再実行をお試しください。<br><br>ヘルプについては、こちらの <a HREF="https://www.mathworks.com/pi_suoe_uai_R2024a_win64">MATLAB Answer</a> を参照してください。<br>Package cannot be read or empty.
(Apr 20, 2024 22:00:38) Exiting with status -2
(Apr 20, 2024 22:00:38) End – Unsuccessful See version information below. I launched as admin, and as not admin. Nothing I could do would work. It would start download and at about a third each time it would fail.
Log file output:
—
(Apr 20, 2024 22:00:22) # Today’s Date:
(Apr 20, 2024 22:00:22) Apr 20, 2024 22:00:22 -0500
(Apr 20, 2024 22:00:22)
(Apr 20, 2024 22:00:22) Starting Add-On Installer
(Apr 20, 2024 22:00:22) System Info
(Apr 20, 2024 22:00:22) OS: Windows Version 10.0 (Build 22631)
(Apr 20, 2024 22:00:22) Arch: amd64
(Apr 20, 2024 22:00:22) Language: ja_JP
(Apr 20, 2024 22:00:22) Release Description: R2024a Update 1
(Apr 20, 2024 22:00:22) Installer’s Build Number: 24.1.0.2568132
(Apr 20, 2024 22:00:22) Session key: 52678390-7d09-4970-8dd5-77d4ef98a1b5
(Apr 20, 2024 22:00:23) Proxy host & port are not active
(Apr 20, 2024 22:00:23) Proxy username & password are not active
(Apr 20, 2024 22:00:23) Proxy validation is successful.
(Apr 20, 2024 22:00:23) C:Program FilesMATLABR2024a is writeable
(Apr 20, 2024 22:00:23) Write permissions validation is successful.
(Apr 20, 2024 22:00:23) Starting download of product meta data
(Apr 20, 2024 22:00:24) Finished downloading product meta data
(Apr 20, 2024 22:00:27) Product data validation is successful.
(Apr 20, 2024 22:00:27) MATLAB detection validation is successful.
(Apr 20, 2024 22:00:27) Install Root: C:Program FilesMATLABR2024a
(Apr 20, 2024 22:00:28) 1%
(Apr 20, 2024 22:00:28) 4%
(Apr 20, 2024 22:00:29) 5%
(Apr 20, 2024 22:00:29) 10%
(Apr 20, 2024 22:00:29) 13%
(Apr 20, 2024 22:00:29) 15%
(Apr 20, 2024 22:00:30) 17%
(Apr 20, 2024 22:00:31) 21%
(Apr 20, 2024 22:00:31) 24%
(Apr 20, 2024 22:00:32) 25%
(Apr 20, 2024 22:00:32) 27%
(Apr 20, 2024 22:00:32) 予期せぬ問題が発生しました。<br>インストーラーの再実行をお試しください。<br><br>ヘルプについては、こちらの <a HREF="https://www.mathworks.com/pi_suoe_uai_R2024a_win64">MATLAB Answer</a> を参照してください。<br>Package cannot be read or empty.
(Apr 20, 2024 22:00:38) Exiting with status -2
(Apr 20, 2024 22:00:38) End – Unsuccessful add-on unknown error MATLAB Answers — New Questions
Warning: Variable appears to change size on every loop iteration (within a script). Consider preallocating for speed.
Hello. I am working on a project where I’ve created a while-loop to calculate 3 power values and 4 cost values. I need to run this loop 60 times. I receive the warning shown in the summary and 3×4 double for my cst variable and 4×3 double in my power variable. I’m trying to store the values in a table so I can see all 60 values of P1, P2, P3, F1, F2, F3, and Ft. I’m very new to MATLAB and I cannot figure out how to get past the fact that I can apparently only get to 12 values each for cost and power. Here is my code:
% Group 2 – Part I.b. Optimal Power Flow with Gradient Search
% Start fresh – clear command window, variables, and plots
clc, clearvars, close all
% Initialize the values
P1 = 300;
P2 = 200;
P3 = 850 – P1 – P2;
n = 0;
k = 60;
alpha = 10;
% Initialize the cost functions
F1 = 561 + 7.92 * P1 + 0.00156 * P1^2;
F2 = 310 + 7.85 * P2 + 0.00194 * P2^2;
F3 = 78 + 7.97 * P3 + 0.00482 * P3^2;
Ft = F1 + F2 + F3;
% Initialize the gradient of the cost functions
gradientF1 = (7.92 + 0.003124 * P1 – 7.97 – 0.00964 * P3);
gradientF2 = (7.85 + 0.00388 * P2 – 7.97 -0.00964 * P3);
gradientF3 = [gradientF1;gradientF2];
while n < k
x = [P1;P2];
n = n + 1;
% Update power values
P1 = P1 – alpha * gradientF1;
P2 = P2 – alpha * gradientF2;
P3 = 850 – P1 – P2;
power(n,1) = P1; %#ok<*SAGROW>
power(n,2) = P2;
power(n,3) = P3;
cst(n,1) = F1;
cst(n,2) = F2;
cst(n,3) = F3;
cst(n,4) = Ft;
end
% Initialize arrays
power_array = zeros(3,60);
cst_array = zeros(4,60);
% Store power values
power_array(k, 🙂 = [P1, P2, P3];
% Store cost values
cst_array(k, 🙂 = [cst1, cst2, cst3, cst4];
% Display results
disp(‘Final Power Values:’);
disp(power_array(end, :));
disp(‘Final Cost Values’);
disp(cst(end, :));
% Plot power values
figure;
plot(3:60, power);
title(‘Power Values’);
xlabel(‘Iterations’);
ylabel(‘Power (MW)’);
legend(‘P1’, ‘P2’, ‘P3’);
% Plot cost values
figure;
plot(4:60, cst);
title(‘Cost Values’);
xlabel(‘Iterations’);
ylabel(‘Cost ($/hr)’);
legend(‘F1’, ‘F2’, ‘F3’, ‘F4’);Hello. I am working on a project where I’ve created a while-loop to calculate 3 power values and 4 cost values. I need to run this loop 60 times. I receive the warning shown in the summary and 3×4 double for my cst variable and 4×3 double in my power variable. I’m trying to store the values in a table so I can see all 60 values of P1, P2, P3, F1, F2, F3, and Ft. I’m very new to MATLAB and I cannot figure out how to get past the fact that I can apparently only get to 12 values each for cost and power. Here is my code:
% Group 2 – Part I.b. Optimal Power Flow with Gradient Search
% Start fresh – clear command window, variables, and plots
clc, clearvars, close all
% Initialize the values
P1 = 300;
P2 = 200;
P3 = 850 – P1 – P2;
n = 0;
k = 60;
alpha = 10;
% Initialize the cost functions
F1 = 561 + 7.92 * P1 + 0.00156 * P1^2;
F2 = 310 + 7.85 * P2 + 0.00194 * P2^2;
F3 = 78 + 7.97 * P3 + 0.00482 * P3^2;
Ft = F1 + F2 + F3;
% Initialize the gradient of the cost functions
gradientF1 = (7.92 + 0.003124 * P1 – 7.97 – 0.00964 * P3);
gradientF2 = (7.85 + 0.00388 * P2 – 7.97 -0.00964 * P3);
gradientF3 = [gradientF1;gradientF2];
while n < k
x = [P1;P2];
n = n + 1;
% Update power values
P1 = P1 – alpha * gradientF1;
P2 = P2 – alpha * gradientF2;
P3 = 850 – P1 – P2;
power(n,1) = P1; %#ok<*SAGROW>
power(n,2) = P2;
power(n,3) = P3;
cst(n,1) = F1;
cst(n,2) = F2;
cst(n,3) = F3;
cst(n,4) = Ft;
end
% Initialize arrays
power_array = zeros(3,60);
cst_array = zeros(4,60);
% Store power values
power_array(k, 🙂 = [P1, P2, P3];
% Store cost values
cst_array(k, 🙂 = [cst1, cst2, cst3, cst4];
% Display results
disp(‘Final Power Values:’);
disp(power_array(end, :));
disp(‘Final Cost Values’);
disp(cst(end, :));
% Plot power values
figure;
plot(3:60, power);
title(‘Power Values’);
xlabel(‘Iterations’);
ylabel(‘Power (MW)’);
legend(‘P1’, ‘P2’, ‘P3’);
% Plot cost values
figure;
plot(4:60, cst);
title(‘Cost Values’);
xlabel(‘Iterations’);
ylabel(‘Cost ($/hr)’);
legend(‘F1’, ‘F2’, ‘F3’, ‘F4’); Hello. I am working on a project where I’ve created a while-loop to calculate 3 power values and 4 cost values. I need to run this loop 60 times. I receive the warning shown in the summary and 3×4 double for my cst variable and 4×3 double in my power variable. I’m trying to store the values in a table so I can see all 60 values of P1, P2, P3, F1, F2, F3, and Ft. I’m very new to MATLAB and I cannot figure out how to get past the fact that I can apparently only get to 12 values each for cost and power. Here is my code:
% Group 2 – Part I.b. Optimal Power Flow with Gradient Search
% Start fresh – clear command window, variables, and plots
clc, clearvars, close all
% Initialize the values
P1 = 300;
P2 = 200;
P3 = 850 – P1 – P2;
n = 0;
k = 60;
alpha = 10;
% Initialize the cost functions
F1 = 561 + 7.92 * P1 + 0.00156 * P1^2;
F2 = 310 + 7.85 * P2 + 0.00194 * P2^2;
F3 = 78 + 7.97 * P3 + 0.00482 * P3^2;
Ft = F1 + F2 + F3;
% Initialize the gradient of the cost functions
gradientF1 = (7.92 + 0.003124 * P1 – 7.97 – 0.00964 * P3);
gradientF2 = (7.85 + 0.00388 * P2 – 7.97 -0.00964 * P3);
gradientF3 = [gradientF1;gradientF2];
while n < k
x = [P1;P2];
n = n + 1;
% Update power values
P1 = P1 – alpha * gradientF1;
P2 = P2 – alpha * gradientF2;
P3 = 850 – P1 – P2;
power(n,1) = P1; %#ok<*SAGROW>
power(n,2) = P2;
power(n,3) = P3;
cst(n,1) = F1;
cst(n,2) = F2;
cst(n,3) = F3;
cst(n,4) = Ft;
end
% Initialize arrays
power_array = zeros(3,60);
cst_array = zeros(4,60);
% Store power values
power_array(k, 🙂 = [P1, P2, P3];
% Store cost values
cst_array(k, 🙂 = [cst1, cst2, cst3, cst4];
% Display results
disp(‘Final Power Values:’);
disp(power_array(end, :));
disp(‘Final Cost Values’);
disp(cst(end, :));
% Plot power values
figure;
plot(3:60, power);
title(‘Power Values’);
xlabel(‘Iterations’);
ylabel(‘Power (MW)’);
legend(‘P1’, ‘P2’, ‘P3’);
% Plot cost values
figure;
plot(4:60, cst);
title(‘Cost Values’);
xlabel(‘Iterations’);
ylabel(‘Cost ($/hr)’);
legend(‘F1’, ‘F2’, ‘F3’, ‘F4’); preallocate, array, store data, display data MATLAB Answers — New Questions