【发布时间】:2016-01-18 07:53:52
【问题描述】:
我试图编写一个代码,它是一个更大程序的一部分,它将在s 的每个点返回z 的值。但是,当我运行代码时,我只得到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)
【问题讨论】:
-
我猜你想在 LHS 上写 z(i)
-
您可能想阅读basics。除此之外,您只运行 s 的最后一个元素的代码。正确的语法是
for i=1:length(s),或者只是使用基于范围的 for 循环作为for i=s,因为您实际上想要对s中的每个元素做一些事情。除此之外,您还需要考虑将索引添加到z。在 Matlab 中,如果您这样写,您将覆盖z(因为您将z从长度为 N 的向量重新定义为标量)。这会在许多编程语言中产生错误,但 Matlab 允许这样做。
标签: matlab if-statement for-loop piecewise