Saving Loop Values to Array with Non-Integer Index
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Phillip Gyasi-Agyei
am 5 Mai 2020
Kommentiert: Phillip Gyasi-Agyei
am 6 Mai 2020
Hi All,
I'm trying to save the results of the iteration loop to an array for plotting, "r" and "x" specifically. My issue is that I can't figure out what to use as an index. My code is below, PLEASE HELP!
R = 0.6;
b = 0.1;
ro = R*b;
a = asind(b);
function [pl] = Geodesic(R, b, ro, a)
% This function plots a geodescic contour
% Initial Conditions
w = 0;
r = R;
x = 0;
Dr = 0;
Dx = 0;
Dw = 1;
hold on
for w = 0:Dw:90
rm = r / ( cosd(w) * (2-tand(a).^2) );
r(w) = r - Dr;
x(w) = x + Dx;
a = asind(ro/r);
Dx = rm * Dw*cosd(w);
Dr = rm * Dw * sind(w);
pl = plot(r(w),x(w),'k.');
title('Geodesic Dome Contour');
xlabel('Distance From Center');
ylabel('Y Position');
end
end
0 Kommentare
Akzeptierte Antwort
KALYAN ACHARJYA
am 5 Mai 2020
Bearbeitet: KALYAN ACHARJYA
am 5 Mai 2020
Make the r and x as output arguments in the function
function [pl,r,x] = Geodesic(R, b, ro, a)
% This function plots a geodescic contour
% Initial Conditions
w = 0;r = R;x = 0;Dr = 0;Dx = 0;Dw = 1;
hold on
for w = 0:Dw:90
rm = r / ( cosd(w) * (2-tand(a).^2) );
r(w) = r - Dr;
x(w) = x + Dx;
a = asind(ro/r);
Dx = rm * Dw*cosd(w);
Dr = rm * Dw * sind(w);
pl = plot(r(w),x(w),'k.');
title('Geodesic Dome Contour');
xlabel('Distance From Center');
ylabel('Y Position');
end
end
Now r and x will be reflected in workspace, now please do whatever you want
3 Kommentare
KALYAN ACHARJYA
am 5 Mai 2020
Bearbeitet: KALYAN ACHARJYA
am 5 Mai 2020
Its the array, in each iteration generates single x and r values, it reflected as an array
%Like x=[6 7 9 -0........]
Modifications
function [pl,r,x] = Geodesic(R, b, ro, a)
% This function plots a geodescic contour
% Initial Conditions
w= 0;r(1)=R;x(1)= 0;Dr = 0;Dx = 0;Dw = 1;
hold on
for w = 2:Dw:90
rm = r(w-1)/ ( cosd(w) * (2-tand(a).^2) );
r(w) = r(w-1) - Dr;
x(w) = x(w-1) + Dx;
a = asind(ro/r(w));
Dx = rm * Dw*cosd(w);
Dr = rm * Dw * sind(w);
end
plot(r,x,'k.');
title('Geodesic Dome Contour');
xlabel('Distance From Center');
ylabel('Y Position');
end
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!