for loop to while loop

2 次查看(过去 30 天)
im trying to convert the code to while loop but im failing to see the mistake
%% while loop
clc
clear
x=16
x = 16
X=[0,10,15,20,22.5,30]
X = 1×6
0 10.0000 15.0000 20.0000 22.5000 30.0000
Y=[0,227.04,362.78,517.35,602.97,901.67]
Y = 1×6
0 227.0400 362.7800 517.3500 602.9700 901.6700
D=length(X)
D = 6
j=1
j = 1
j1=1
j1 = 1
i=1
i = 1
while j<D
z(i)=1
while j1<D
if i~=j
z(i)=z(i)*(x-X(j))/(X(i)-X(j))
end
j1=j1+1
end
z(i)=z(i)*Y(i)
j=j+1
i=i+1
end
z = 1
j1 = 2
j1 = 3
j1 = 4
j1 = 5
j1 = 6
z = 0
j = 2
i = 2
z = 1×2
0 1
z = 1×2
0 227.0400
j = 3
i = 3
z = 1×3
0 227.0400 1.0000
z = 1×3
0 227.0400 362.7800
j = 4
i = 4
z = 1×4
0 227.0400 362.7800 1.0000
z = 1×4
0 227.0400 362.7800 517.3500
j = 5
i = 5
z = 1×5
0 227.0400 362.7800 517.3500 1.0000
z = 1×5
0 227.0400 362.7800 517.3500 602.9700
j = 6
i = 6
sum(z)
ans = 1.7101e+03
%% for loop
clc
clear
x=16
x = 16
X=[0,10,15,20,22.5,30]
X = 1×6
0 10.0000 15.0000 20.0000 22.5000 30.0000
Y=[0,227.04,362.78,517.35,602.97,901.67]
Y = 1×6
0 227.0400 362.7800 517.3500 602.9700 901.6700
D=length(X)
D = 6
for i=1:D
z(i)=1
for j=1:D
if i~=j
z(i)=z(i)*(x-X(j))/(X(i)-X(j))
end
end
z(i)=z(i)*Y(i)
end
z = 1
z = -0.6000
z = 0.0400
z = 0.0080
z = 0.0023
z = 0.0011
z = 0
z = 1×2
0 1
z = 1×2
0 1.6000
z = 1×2
0 -0.3200
z = 1×2
0 -0.1280
z = 1×2
0 -0.0666
z = 1×2
0 -0.0466
z = 1×2
0 -10.5782
z = 1×3
0 -10.5782 1.0000
z = 1×3
0 -10.5782 1.0667
z = 1×3
0 -10.5782 1.2800
z = 1×3
0 -10.5782 1.0240
z = 1×3
0 -10.5782 0.8875
z = 1×3
0 -10.5782 0.8283
z = 1×3
0 -10.5782 300.4915
z = 1×4
0 -10.5782 300.4915 1.0000
z = 1×4
0 -10.5782 300.4915 0.8000
z = 1×4
0 -10.5782 300.4915 0.4800
z = 1×4
0 -10.5782 300.4915 0.0960
z = 1×4
0 -10.5782 300.4915 0.2496
z = 1×4
0 -10.5782 300.4915 0.3494
z = 1×4
0 -10.5782 300.4915 180.7828
z = 1×5
0 -10.5782 300.4915 180.7828 1.0000
z = 1×5
0 -10.5782 300.4915 180.7828 0.7111
z = 1×5
0 -10.5782 300.4915 180.7828 0.3413
z = 1×5
0 -10.5782 300.4915 180.7828 0.0455
z = 1×5
0 -10.5782 300.4915 180.7828 -0.0728
z = 1×5
0 -10.5782 300.4915 180.7828 -0.1359
z = 1×5
0 -10.5782 300.4915 180.7828 -81.9596
z = 1×6
0 -10.5782 300.4915 180.7828 -81.9596 1.0000
z = 1×6
0 -10.5782 300.4915 180.7828 -81.9596 0.5333
z = 1×6
0 -10.5782 300.4915 180.7828 -81.9596 0.1600
z = 1×6
0 -10.5782 300.4915 180.7828 -81.9596 0.0107
z = 1×6
0 -10.5782 300.4915 180.7828 -81.9596 -0.0043
z = 1×6
0 -10.5782 300.4915 180.7828 -81.9596 0.0037
z = 1×6
0 -10.5782 300.4915 180.7828 -81.9596 3.3342
sum(z)
ans = 392.0706

采纳的回答

Eric Delgado
Eric Delgado 2023-1-9
Try this...
x=16;
X=[0,10,15,20,22.5,30];
Y=[0,227.04,362.78,517.35,602.97,901.67];
D=length(X);
% WHILE LOOPS
i=1;
while i <= D
z1(i)=1;
j = 1;
while j <= D
if i~=j
z1(i) = z1(i) * (x-X(j))/(X(i)-X(j));
end
j = j+1;
end
z1(i) = z1(i)*Y(i);
i=i+1;
end
% FOR LOOPS
for i = 1:D
z2(i) = 1;
for j = 1:D
if i ~= j
z2(i) = z2(i) * (x-X(j))/(X(i)-X(j));
end
end
z2(i) = z2(i)*Y(i);
end
isequal(z1, z2)
ans = logical
1

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by