F(i +N) function and loop

Hi, I need help with creating a code that will run from a set value of eta to a set value of N in intervals of 0.5 as a value of i so basically i = eta:0.5:N. In the loop I need to run a code that runs the equation f(i+eta) = f(i) + eta*f1(i); and do that for f - f3 values. This is the code I have so far. Any insight on how to get something similar to this to work in intervals of 0.5 would be greatly appreciated.
%Initial Variables
eta = 0;
N = 10;
alpha = 0.01;
%Boundary Conditions
f = 0;
f1 = 0;
f2 = alpha;
f3 = 0;
if i == eta:0.5:N
f(i+N) = f(i)+N*f1(i);
f1(i+N) = f1(i)+N*f2(i);
f2(i+N) = f2(i)+N*f3(i);
f3(i+N) = -0.5*f(i+N)*f2(i+N);
end

1 个评论

Can you explain what are f1,f2,f3? Are those supposed to be functions?

请先登录,再进行评论。

回答(1 个)

%{
if i == eta:0.5:N
%}
You have not have assigned a value to i before that point, so i will be its default sqrt(-1)
eta:0.5:N is going to be a vector with values 0, 0.5, 1, 1.5, up to 9.5 and then 10.
So you are comparing sqrt(-1) to the vector 0, 0.5, 1, ... 10. Those are all non-negative real values, so none of them is going to be equal to sqrt(-1) .
In MATLAB, when you use if and a vector of values, the condition is considered true only if all of the values are non-zero. But in this case, not only are some of the values zero (false), but indeed all of them are 0 (false).
Therefore your if test is going to fail, so the series of assignments will not be done.
%Initial Variables
eta = 0;
N = 10;
alpha = 0.01;
%Boundary Conditions
f = 0;
f1 = 0;
f2 = alpha;
f3 = 0;
i = eta:0.5:N;
for K = 1:length(i)
f(K+2*N) = f(K)+N*f1(K);
f1(K+2*N) = f1(K)+N*f2(K);
f2(K+2*N) = f2(K)+N*f3(K);
f3(K+2*N) = -0.5*f(K+2*N)*f2(K+2*N);
end
plot([f;f1;f2;f3].')
legend({'f', 'f1', 'f2', 'f3'})

类别

帮助中心File Exchange 中查找有关 Special Functions 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by