How to change two variables in one loop?

조회 수: 5 (최근 30일)
Ani Asoyan
Ani Asoyan 2020년 7월 13일
댓글: Ani Asoyan 2020년 7월 14일
Hi... I have something like this..
d(1)=0.3
a(1)=0.5
for i=1:5
/some code/
a(i+1)=a(i)-0.5*p
d(i+1)=d(i)-0.3*p+0.5*N
end
p and N are parameters,, and I want to change a and d variables in this loop simultaneously.. how can I do that? Thank you .

답변 (1개)

the cyclist
the cyclist 2020년 7월 13일
편집: the cyclist 2020년 7월 13일
There are many things about your question that I don't fully understand. The least important is whether you meant the loop to go over k rather than i. If not, then I don't understand how i is supposed to be used.
Most importantly, I don't really understand what you mean by "simultaneously" here. Do you mean that you want to somehow execute
a(k+1)=a(k)-0.5*p
and
d(k+1)=d(k)-0.3*p+0.5*N
in a single line of code?
Why? These two lines of code do not affect each, and could be executed in either order, with same result.
You could define a function that returns a and d "simultaeously", but I'm not sure of the point of that.
Maybe I just don't fully understand your question.
  댓글 수: 5
the cyclist
the cyclist 2020년 7월 14일
편집: the cyclist 2020년 7월 14일
When I turn that code, I get a different error. When MATLAB calls the line
g=(u_g( Z(:,1), Z(:,3), Z(:,2) ).');
I get the error
Unrecognized function or variable 'u_p'.
because
u_g = @(x, x_e, N)(-0.5*a.*x.^2+b*(x-x_e)-c*(N.^l)+e*u_p(x,x_e,N,d(k)));
and u_p is not defined in your code.
Ani Asoyan
Ani Asoyan 2020년 7월 14일
yeah sorry because there's a function called u_p ,,,here's its code
function u=u_p(x,x_e,N,d)
d=0.7-0.6*x+0.4*N
u = -(x-x_e).^2+N.*d;
end

댓글을 달려면 로그인하십시오.

카테고리

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