Matlab Guide with 2 plots

3 views (last 30 days)
Tim Schaller
Tim Schaller on 10 Nov 2022
Commented: Rik on 10 Nov 2022
Hi guys,
ich created a MATLAB GUIDE, but somehow the second plot does not work.
I don't have a clue, what i was doing wrong, but it should like in the Code below.
Thanks in advance.
n = 3;
n1 = n-1;
a = 20; % Breite des Krummers entlang x
b = 10; % Höhe des Krümmers entlang y
P = [0 b;0 0;a 0]; % Punkte für Beziér-Spline Plot
T = 15; % Anazhl an Teilpunkten für den Plot
H = 8; % Höhe des Ausgangs
R = 2; % Radius des Eingangs
h = 6; % Breite des Ausgangs
syms t s(t)
B = bernsteinMatrix(n1,t);
bezierCurve = B*P;
s(t) = int(norm(diff(bezierCurve)),0,t);
snum = linspace(0,s(1),T);
for i = 1:T
tnum(i) = vpasolve(snum(i)==s(t),t); %vpasolve löst Gleichungen mit Symbolvariablen
end
px = double(subs(bezierCurve(:,1),t,tnum)).';
py = zeros(T,1);
pz = double(subs(bezierCurve(:,2),t,tnum)).';
normalToCurve = diff(bezierCurve)*[0 1;-1 0];
normalToCurve = normalToCurve/norm(normalToCurve);
newNormalToCurve = [double(subs(normalToCurve(1),t,tnum))' double(subs(normalToCurve(2),t,tnum))'];
newNormalToCurvex = newNormalToCurve(:,1);
newNormalToCurvez = newNormalToCurve(:,2);
plot3(px,py,pz,'b-o',"MarkerSize",4)
axis equal
axis tight
grid on
hold on
%%%%%%%%%%%Obere Linie
for i = 1:T
S = double(s((i-1)/(T-1)));
d = R*(1-S/double(s(1)))+(H/2)*S/double(s(1));
delta(i) = d;
end
delta = delta';
pxnew1 = px+newNormalToCurvex.*delta;
pznew1 = pz+newNormalToCurvez.*delta;
for i = 1:T
S = double(s((i-1)/(T-1)));
rhilfe = (h/2)*S/double(s(1));
r(i) = rhilfe;
end
r = r';
for i = 1:2
pynew1 = r*(-1)^i;
plot3(pxnew1,pynew1,pznew1,'r-o','MarkerSize',4)
end
pynew1a = - r;
pynew1b = r;
%%%%%%%%%%Untere Linie
delta = - delta;
pxnew2 = px+newNormalToCurvex.*delta;
pznew2 = pz+newNormalToCurvez.*delta;
for i = 1:2
pynew1 = r*(-1)^i;
plot3(pxnew2,pynew1,pznew2,'r-o','MarkerSize',4)
end
pynew2a = pynew1a;
pynew2b = pynew1b;
%%%%%%%%%%seitliche Linien (schwarz)
for i = 1:T
S = double(s((i-1)/(T-1)));
chilfe = R*(1-S/double(s(1)))+(h/2)*S/double(s(1));
c(i) = chilfe;
end
for i = 1:T
S = double(s((i-1)/(T-1)));
lhilfe = H/2*S/double(s(1));
l(i) = lhilfe;
end
l = l';
c = c';
pynew3 = ones(T,1).*c;
for i = 1:2
pxnew3 = px+newNormalToCurvex.*l*(-1)^i;
pznew3 = pz+newNormalToCurvez.*l*(-1)^i;
plot3(pxnew3,pynew3,pznew3,'k-o','MarkerSize',4)
pynew3 = -pynew3;
plot3(pxnew3,pynew3,pznew3,'k-o','MarkerSize',4)
end
pxnew3a = px-newNormalToCurvex.*l;
pxnew3b = px+newNormalToCurvex.*l;
pznew3a = pz-newNormalToCurvez.*l;
pznew3b = pz+newNormalToCurvez.*l;
pynew3a = pynew3;
pynew3b = -pynew3;
%%%%%%%%%%%%4Ecken
%%%vorne links/rechts (magenta)
Rnewx = R*cos(pi/4);
Rnewy = R*cos(pi/4);
for i = 1:T
S = double(s((i-1)/(T-1)));
jhilfe = Rnewx*(1-S/double(s(1)))+(H/2)*S/double(s(1));
j(i) = jhilfe;
end
for i = 1:T
S = double(s((i-1)/(T-1)));
ehilfe = Rnewy*(1-S/double(s(1)))+(h/2)*S/double(s(1));
e(i) = ehilfe;
end
j = j';
e = e';
pxnew4 = px+newNormalToCurvex.*j;
pznew4 = pz+newNormalToCurvez.*j;
for i = 1:2
pynew4 = ones(T,1).*e*(-1)^i;
plot3(pxnew4,pynew4,pznew4,'m-o',"MarkerSize",4)
end
pynew4a = -ones(T,1).*e;
pynew4b = ones(T,1).*e;
%%%%%vorne links/recht(grün)
pxnew5 = px-newNormalToCurvex.*j;
pznew5 = pz-newNormalToCurvez.*j;
for i = 1:2
pynew5 = ones(T,1).*e*(-1)^i;
plot3(pxnew5,pynew5,pznew5,'g-o',"MarkerSize",4)
end
pynew5a = pynew4a;
pynew5b = pynew4b;
hold off
pxgesamt = [px;pxnew1; pxnew1; pxnew2; pxnew2; pxnew3a;pxnew3b;pxnew3a;pxnew3b;pxnew4; pxnew4; pxnew5; pxnew5];
pygesamt = [py;pynew1a;pynew1b;pynew2a;pynew2b;pynew3a;pynew3a;pynew3b;pynew3b;pynew4a;pynew4b;pynew5a;pynew5b];
pzgesamt = [pz;pznew1; pznew1; pznew2; pznew2; pznew3a;pznew3b;pznew3a;pznew3b;pznew4; pznew4; pznew5; pznew5];
pgesamt = [pxgesamt pygesamt pzgesamt];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Plot der Draufsicht
arclen = arclength(px,pz);
S = linspace(0,arclen,T);
%obere Linien
p2y1 = linspace(0,h/2,T);
for i = 1:T
z2t = R*(1-S(i)/arclen)+(H/2)*S(i)/arclen;
z2(i) = z2t;
end
z2 = z2';
p2z1 = z2;
p2y1b = -p2y1;
%untere Linien
p2y2 = p2y1;
p2z2 = -p2z1;
p2y2b = p2y1b;
%seitliche Linien
for i = 1:T
c2hilfe = R*(1-S(i)/arclen)+h/2*(S(i)/arclen);
c2(i) = c2hilfe;
end
c2 = c2';
p2y3 = c2;
p2z3 = linspace(0,H/2,T);
p2z3b = -p2z3;
p2y3b = -p2y3;
%oben vorne/hinten (magenta)
Rnew = R*cos(pi/4);
for i = 1:T
j2hilfe = Rnew*(1-S(i)/arclen)+(H/2)*S(i)/arclen;
j2(i) = j2hilfe;
end
for i = 1:T
e2hilfe = Rnew*(1-S(i)/arclen)+(h/2)*S(i)/arclen;
e2(i) = e2hilfe;
end
j2 = j2';
e2 = e2';
p2y4 = e2;
p2z4 = j2;
p2y4b = -p2y4;
% unten vorne/hinten (grün)
p2z5 = -p2z4;
%%%%Plot der Punkte
for i = 1:T
pyt = [p2y1(i); p2y4(i); p2y3(i); p2y3(i); p2y4(i); p2y2(i); p2y2b(i); p2y4b(i);
p2y3b(i); p2y3b(i); p2y4b(i); p2y1b(i); p2y1(i)];
pzt = [p2z1(i); p2z4(i); p2z3(i); p2z3b(i); p2z5(i); p2z2(i); p2z2(i); p2z5(i);
p2z3b(i); p2z3(i); p2z4(i); p2z1(i); p2z1(i)];
plot(pyt,pzt,'o',"MarkerSize",3,"MarkerEdgeColor",'k')
hold on
end
%%%%Plot der Geraden Linien
for i = 1:T
pyt = [p2y1(i);p2y1b(i)];
pzt = [p2z1(i);p2z1(i)];
plot(pyt,pzt,'k-',"MarkerSize",3,"MarkerEdgeColor",'k')
hold on
pyt = [p2y3(i); p2y3(i)];
pzt = [p2z3(i); p2z3b(i)];
plot(pyt,pzt,'k-',"MarkerSize",3,"MarkerEdgeColor",'k')
pyt = [p2y2(i); p2y2b(i)];
pzt = [p2z2(i); p2z2(i)];
plot(pyt,pzt,'k-',"MarkerSize",3,"MarkerEdgeColor",'k')
pyt = [p2y3b(i); p2y3b(i)];
pzt = [p2z3b(i); p2z3(i)];
plot(pyt,pzt,'k-',"MarkerSize",3,"MarkerEdgeColor",'k')
end
axis equal
axis tight
grid on
%%%%%%%%%%%%%%%%%%%%%%%Ecken abrunden%%%%%%%%%%%%%%%%%%%%%
%radius (Funktion für linear sinkenden Radius)
for i = 1:T
radius = R*(1-S(i)/arclen);
rad(i) = radius;
end
rad = rad';
% Mittelpunkt
for i = 1:T
MPxhilfe = h/2*S(i)/arclen;
MPx(i) = MPxhilfe;
MPyhilfe = H/2*S(i)/arclen;
MPy(i) = MPyhilfe;
end
MPx = MPx';
MPy = MPy';
MP = [MPx MPy];
% Winkel
deg = 90:-1:0;
for i = 1:T
xc = MP(i,1)+rad(i)*cosd(deg);
yc = MP(i,2)+rad(i)*sind(deg);
plot(xc,yc,'Color','k')
end
for i = 1:T
xc = MP(i,1)+rad(i)*cosd(deg);
yc = -MP(i,2)-rad(i)*sind(deg);
plot(xc,yc,'Color','k')
end
for i = 1:T
xc = -MP(i,1)-rad(i)*cosd(deg);
yc = -MP(i,2)-rad(i)*sind(deg);
plot(xc,yc,'Color','k')
end
for i = 1:T
xc = -MP(i,1)-rad(i)*cosd(deg);
yc = MP(i,2)+rad(i)*sind(deg);
plot(xc,yc,'Color','k')
end
hold off
function [arclen,seglen] = arclength(px,py,varargin)
if nargin < 2
error('ARCLENGTH:insufficientarguments', ...
'at least px and py must be supplied')
end
n = length(px);
if ~isvector(px) || ~isvector(py) || (length(py) ~= n)
error('ARCLENGTH:improperpxorpy', ...
'px and py must be vectors of the same length')
elseif n < 2
error('ARCLENGTH:improperpxorpy', ...
'px and py must be vectors of length at least 2')
end
data = [px(:),py(:)];
method = 'linear';
if numel(varargin) > 0
for i = 1:numel(varargin)
arg = varargin{i};
if ischar(arg)
validmethods = {'linear' 'pchip' 'spline'};
ind = strmatch(lower(arg),validmethods);
if isempty(ind) || (length(ind) > 1)
error('ARCLENGTH:invalidmethod', ...
'Invalid method indicated. Only ''linear'',''pchip'',''spline'' allowed.')
end
method = validmethods{ind};
else
if numel(arg) ~= n
error('ARCLENGTH:inconsistentpz', ...
'pz was supplied, but is inconsistent in size with px and py')
end
data = [data,arg(:)];
end
end
end
nd = size(data,2);
seglen = sqrt(sum(diff(data,[],1).^2,2));
arclen = sum(seglen);
if strcmpi(method,'linear')
return
end
chordlen = seglen;
spl = cell(1,nd);
spld = spl;
diffarray = [3 0 0;0 2 0;0 0 1;0 0 0];
for i = 1:nd
switch method
case 'pchip'
spl{i} = pchip([0;cumsum(chordlen)],data(:,i));
case 'spline'
spl{i} = spline([0;cumsum(chordlen)],data(:,i));
nc = numel(spl{i}.coefs);
if nc < 4
spl{i}.coefs = [zeros(1,4-nc),spl{i}.coefs];
spl{i}.order = 4;
end
end
xp = spl{i};
xp.coefs = xp.coefs*diffarray;
xp.order = 3;
spld{i} = xp;
end
polyarray = zeros(nd,3);
for i = 1:spl{1}.pieces
for j = 1:nd
polyarray(j,:) = spld{j}.coefs(i,:);
end
seglen(i) = quadgk(@(t) segkernel(t),0,chordlen(i));
end
arclen = sum(seglen);
function val = segkernel(t)
val = zeros(size(t));
for k = 1:nd
val = val + polyval(polyarray(k,:),t).^2;
end
val = sqrt(val);
end
end
  1 Comment
Rik
Rik on 10 Nov 2022
Try to make a MWE so we can run your code without any other dependencies and can reproduce your issue. What exactly is your problem? Should the two images be the same?
With long code sections, I would generally recommend attaching functions. The run code option will still use them.

Sign in to comment.

Answers (0)

Categories

Find more on Thermal Analysis in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!