Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab 循环没有为分段函数返回正确的值_Matlab_If Statement_For Loop_Piecewise - Fatal编程技术网

Matlab 循环没有为分段函数返回正确的值

Matlab 循环没有为分段函数返回正确的值,matlab,if-statement,for-loop,piecewise,Matlab,If Statement,For Loop,Piecewise,我曾尝试编写一个代码,作为更大程序的一部分,它将在s的每个点返回z的值。但是,当我运行代码时,我只得到z=0,或者如果忽略了lastelse,则代码返回零向量 有人知道我哪里出错了吗?我从这个角度使用了方法1。任何帮助都将不胜感激,我已经努力得到这项工作几个月了 % clc;close all; %// not generally appreciated %initial values b=1.25; h=0.313; %define the s coordinate s= 0:0.001:2

我曾尝试编写一个代码,作为更大程序的一部分,它将在
s
的每个点返回
z
的值。但是,当我运行代码时,我只得到
z=0
,或者如果忽略了last
else
,则代码返回零向量

有人知道我哪里出错了吗?我从这个角度使用了方法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
%clc;全部关闭;%//不被普遍认可
%初始值
b=1.25;
h=0.313;
%定义s坐标
s=0:0.001:2*(b+h);
%为速度创建零矩阵
z=零(大小);
%在s坐标的每个点上计算z
对于i=长度(s)
如果0
b=1.25;
h=0.313;
%定义s坐标
s=0:0.001:2*(b+h);
%为速度创建零矩阵
z=零(大小);
%在s坐标的每个点上计算z
对于ii=1:长度(s)

如果0则代码存在许多问题。您需要将索引赋值为
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
%//初始值
b=1.25;
h=0.313;
%//定义s坐标
s=0:0.001:2*(b+h);
%//为速度创建零矩阵
z=零(大小);
%//在s坐标的每个点上计算z
对于i=1:长度(s)

如果0,我猜你想在左边写z(i),你可能想在左边读。除此之外,您只运行s的最后一个元素的代码。正确的语法是i=1的
:length(s)
,或者简单地将基于范围的for循环用作i=s的
,因为您实际上希望对
s
中的每个元素执行一些操作。除此之外,您还需要考虑将索引添加到<代码> z < /代码>。在Matlab中,如果您这样编写,您将覆盖
z
(因为您将
z
从长度为N的向量重新定义为标量)。这会在许多编程语言中产生错误,但Matlab允许这样做。
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
%// 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
%// 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);