İnvisible plots and a error

조회 수: 3 (최근 30일)
Ömer Fatih Özdemir
Ömer Fatih Özdemir 2022년 5월 22일
댓글: Walter Roberson 2022년 5월 24일
Guys what is wrong with my matlab codes? I could not take plots from first two figure and i take 'Array indices must be positive integers or logical values.' error from last two for loops.
clear;
clc;
y0=3;
x0=1:5;
for i=1:5
l(i)=sqrt(x0(i)^2+y0.^2);
Q(i)= atan(y0/x0(i));
end
plot(x0(i),l)
hold on
plot(x0(i),Q)
hold on
figure(1)
l1=3.5;
l2=3.5;
for a=1:5
q2(a)=acos((x0(a)^2+y0^2-l1^2-l2^2)/(2*l1*l2));
q1(a)=atan(y0/x0(a))-atan((l2*sin(q2)/(l1+l2*cos(q2))));
end
plot(x0(a),q1)
hold on
plot(x0(a),q2)
hold on
figure(2)
c2=45;
i1=2;
i2=3;
i3=1.5;
c1=0:180;
for k=0:180
x(k)=(i2+i3)*cos(c2)*cos(c1(k));
y(k)=(i2+i3)*cos(c2)*sin(c1(k));
z(k)=i1+(i2+i3)*sin(c2);
end
plot(c1(k),x)
hold on
plot(c1(k),y)
hold on
plot(c1(k),z)
hold on
figure(3)
i3_1=0.5:2;
c1_1=0:360;
for b=0:360
x(b)=(i2+i3_1)*cos(c2)*cos(c1(b));
y(b)=(i2+i3_1)*cos(c2)*sin(c1(b));
z(b)=i1+(i2+i3_1)*sin(c2);
end
plot(c1(b),x)
hold on
plot(c1(b),y)
hold on
plot(c1(b),z)
hold on
figure(4)

채택된 답변

Walter Roberson
Walter Roberson 2022년 5월 22일
clear;
clc;
y0=3;
x0=1:5;
for i=1:5
l(i)=sqrt(x0(i)^2+y0.^2);
Q(i)= atan(y0/x0(i));
end
figure(1)
plot(x0, l, 'DisplayName', 'l')
hold on
plot(x0, Q, 'DisplayName', 'Q')
hold on
legend show
figure(2)
l1=3.5;
l2=3.5;
for a=1:5
q2(a)=acos((x0(a)^2+y0^2-l1^2-l2^2)/(2*l1*l2));
q1(a)=atan(y0/x0(a))-atan((l2*sin(q2)/(l1+l2*cos(q2))));
end
plot(x0, q1, 'DisplayName', 'q1')
hold on
plot(x0, q2, 'DisplayName', 'q2')
hold on
legend show
figure(3)
c2=45;
i1=2;
i2=3;
i3=1.5;
c1=0:180;
clear x y z
for k=0:180
x(k+1)=(i2+i3)*cos(c2)*cos(c1(k+1));
y(k+1)=(i2+i3)*cos(c2)*sin(c1(k+1));
z(k+1)=i1+(i2+i3)*sin(c2);
end
plot(c1, x, 'DisplayName', 'x')
hold on
plot(c1, y, 'DisplayName', 'y')
hold on
plot(c1, z, 'DisplayName', 'z')
hold on
legend show
figure(4)
i3_1=0.5:2;
c1_1=0:360;
clear x y z
for b=0:360
x(b+1,:)=(i2+i3_1)*cos(c2)*cos(c1_1(b+1));
y(b+1,:)=(i2+i3_1)*cos(c2)*sin(c1_1(b+1));
z(b+1,:)=i1+(i2+i3_1)*sin(c2);
end
plot(c1_1, x, 'DisplayName', 'x')
hold on
plot(c1_1, y, 'DisplayName', 'y')
hold on
plot(c1_1, z, 'DisplayName', 'z')
hold on
legend show
The last of those is a bit odd because your i3_1 is a vector consisting of [0.5, 1.5] since you have 0.5:2 but the default increment is 1.
In all of your other plots, the i3 or equivalent is a scalar; this is the only plot where it is a vector.
  댓글 수: 7
Ömer Fatih Özdemir
Ömer Fatih Özdemir 2022년 5월 24일
x = zeros(size(c1));
y = zeros(size(c1));
z = zeros(size(c1));
for k = 1 : numel(c1)
x(k) = (i2+i3)*cosd(c2)*cosd(c1(k));
y(k) = (i2+i3)*cosd(c2)*sind(c1(k));
z(k) = i1+(i2+i3)*sind(c2);
end
i used this for the figure 3.
Is there a possibility to do figure 4 as only b will be in the brackets? Because i have to explain this work in video and i am not good enough in matlab to understand your explanations sorry it is a bit complicated to me :')
Walter Roberson
Walter Roberson 2022년 5월 24일
for b=1:numel(c1_1)
x(b,:)=(i2+i3_1)*cos(c2)*cos(c1_1(b));
y(b,:)=(i2+i3_1)*cos(c2)*sin(c1_1(b));
z(b,:)=i1+(i2+i3_1)*sin(c2);
end

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by