循环不会返回分段函数的正确值

时间:2016-01-18 07:53:53

标签: matlab if-statement for-loop piecewise

我试图编写一个代码,它是一个更大的程序的一部分,它会在z的每个点返回s的值。但是,当我运行代码时,我只获得z=0,或者如果忽略最后else,则代码返回零向量。

有人知道我弄错了吗?我使用了source中的方法1。任何帮助将不胜感激,我已经尝试了几个月的工作。

% clc;close all; %// not generally appreciated
%initial values
b=1.25;
h=0.313;

%define the s coordinate
s= 0:0.001:2*(b+h); 

%create zero matrix for speed
z=zeros(size(s));

%calculate z at every point of s coordinate
for i =length(s)
   if 0 <= s(i) && s(i) <=b   %0<=s<=b
       z=0.5*h;

   elseif b <= s(i) && s(i) <=(b+h)   %b<=s<=(b+h)
       z=0.5*h+((-0.5*h)/(b+h-b))*(s-b);

   elseif b <= s(i) && s(i) <=(b+h)    %(h+b)<=s<=(b+h)
       z=-0.5*h;

   elseif b <= s(i) && s(i) <=(b+h)    %(h+2b)<=s<=(2b+2h)
       z=-0.5*h+((-0.5*h)/(b+h-b))*(s-b);
   else z=0;

   end
end

为了进一步参考,这解决了我的问题。谢谢@Dan!

 %// initial values
   b=1.25;
   h=0.313;
   %// define the s coordinate
   s= 0:0.001:2*(b+h);
   %// Create z
   z = zeros(size(s));
   idx1 = 0 <= s & s <=b;
   idx2 = b <= s & s <=(b+h);
   idx3 = (b+h) <= s & s <= (2*b+h);
   idx4 = (2*b+h) <= s & s <=(2*b+2*h);
   z(idx1) = 0.5*h;
   z(idx2) = 0.5*h+((-0.5*h-0.5*h)/(b+h-b))*(s(idx2)-b);
   z(idx3) = -0.5*h;
   z(idx4) =-0.5*h+((0.5*h+0.5*h)/((2*b+2*h-b)-(h+b+b)))*(s(idx4)-b)

2 个答案:

答案 0 :(得分:1)

您的代码存在许多问题。您需要分配一个z的索引,否则您每次都会覆盖一个标量(即z(i)=...)。你需要遍历一个向量,所以fori=1:length(s)和你的最后三个循环条件是相同的!

%// initial values
b=1.25;
h=0.313;

%// define the s coordinate
s= 0:0.001:2*(b+h); 

%// create zero matrix for speed
z=zeros(size(s));

%// calculate z at every point of s coordinate
for i = 1:length(s)
   if 0 <= s(i) && s(i) <=b                   %// 0<=s<=b
       z=0.5*h;

   elseif b <= s(i) && s(i) <=(b+h)           %// b<=s<=(b+h)
       z(i)=0.5*h+((-0.5*h)/(b+h-b))*(s-b);

   elseif (b+h) <= s(i) && s(i) <= (2*b+h)    %// (h+b)<=s<=(2b+h)
       z(i)=-0.5*h;

   elseif (2*b+h) <= s(i) && s(i) <=(2*b+2*h) %// (h+2b)<=s<=(2b+2h)
       z(i)=-0.5*h+((-0.5*h)/(b+h-b))*(s-b);
   else z(i)=0;

   end
end

总而言之,在MATLAB中你甚至根本不需要一个循环来做这个,通常最好不要使用一个:

%// initial values
b=1.25;
h=0.313;
%// define the s coordinate
s= 0:0.001:2*(b+h);
%// Create z
z = zeros(size(s));
idx1 = 0 <= s && s <=b;
idx2 = b <= s && s <=(b+h);
idx3 = (b+h) <= s && s <= (2*b+h);
idx4 = (2*b+h) <= s && s <=(2*b+2*h);
z(idx1) = 0.5*h;
z(idx2) = 0.5*h+((-0.5*h)/(b+h-b))*(s(idx2)-b);
z(idx3) = -0.5*h;
z(idx4) = -0.5*h+((-0.5*h)/(b+h-b))*(s(idx4)-b);

答案 1 :(得分:0)

b=1.25;
h=0.313;

%define the s coordinate
s= 0:0.001:2*(b+h); 

%create zero matrix for speed
z=zeros(size(s));

%calculate z at every point of s coordinate
for ii =1:length(s)
   if 0 <= s(ii) && s(ii) <=b   %0<=s<=b
       z(ii)=0.5*h;

   elseif b <= s(ii) && s(ii) <=(b+h)   %b<=s<=(b+h)
       z(ii)=0.5*h+((-0.5*h)/(b+h-b))*(s(ii)-b);

   elseif b <= s(ii) && s(ii) <=(b+h)    %(h+b)<=s<=(b+h)
       z(ii)=-0.5*h;

   elseif b <= s(ii) && s(ii) <=(b+h)    %(h+2b)<=s<=(2b+2h)
       z(ii)=-0.5*h+((-0.5*h)/(b+h-b))*(s(ii)-b);
   else z(ii)=0;

   end
end

让你的for循环超过1次迭代,因此for ii = 1:length(s)

使用每个元素的分配,因此z(ii) = some function

Don't use i as a variable.