How to change two variables in one loop?

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 .

Answers (1)

the cyclist
the cyclist on 13 Jul 2020
Edited: the cyclist on 13 Jul 2020
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 Comments

oh my bad.. sorry I made a mistake.. k and i should be the same.. it's not that important,, imagine it's i ......
well a and d are variables that go to some functions. I want to change their values in every iteration of the loop. And I want them to change simultaneously. I can attach full code if you want
OK, so you edited your code to fix up the i/k issue. Cool.
But, I still don't understand what you mean by "simultaneously". The way you have written your code, those two variable do change in every iteration of the loop. If you have
for i = 1:5
% some code and functions
a(i+1)=a(i)-0.5*p;
d(i+1)=d(i)-0.3*p+0.5*N;
% some other code and functions
end
then a and d are effectively simultaneously changed, with respect to all the other code.
Maybe attaching the full code will help. But try to explain more precisely what you mean by "simultaneously", and be specific about what the current code fails to do, that you want it to do.
sorry for the late reply.. here's the full code.
clc
x=[0.5 , 1]
x_e=[0.5, 1]
N=[0.5, 1]
[mmm,nnn,ppp] = ndgrid(x,x_e,N);
Z = [mmm(:),nnn(:),ppp(:)];
d(1)=0.1
a(1)=0.5
for k=1:3
b=1.2; c=1.5; e=1.2; l=2;
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)));
g=(u_g( Z(:,1), Z(:,3), Z(:,2) ).');
p=(u_p(Z(:,1), Z(:,3), Z(:,2), d(k)).');
gp=reshape(g,[numel(x)*numel(N),numel(x_e)]).' ;
pp=reshape(p, [numel(x)*numel(N),numel(x_e)]).' ;
num2strcell = @(m) arrayfun(@num2str, m, 'UniformOutput', false);
payoffs=strcat(num2strcell(gp), num2strcell(pp))
n=numel(x)*numel(N);
m=numel(x_e);
for i=1:n
A(i)=max(pp(:,i));
attainedA=(max(pp, [] ,1)==pp);
end
for j=1:m
B(j)=max(gp(j,:));
attainedB=( max(gp,[],2)==gp );
end
result = payoffs(attainedA & attainedB)
gov=gp(attainedA & attainedB)
pub=pp(attainedA & attainedB)
mnpg=[mmm(:), nnn(:), ppp(:), g(:)];
resval=mnpg(ismember(mnpg(:,4),gov),1:3)
pival=resval(1)
Nval=resval(2)
d(k+1)=d(k)-0.2*pival+3*Nval
a(k+1)=a(k)-0.5*pival
end
when I run it, it gives me this error..
Error using reshape
To RESHAPE the number of elements must not change.
Error in dynamic (line 21)
gp=reshape(g,[numel(x)*numel(N),numel(x_e)]).' ;
what can I do to make g remain the same
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.
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

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 13 Jul 2020

Commented:

on 14 Jul 2020

Community Treasure Hunt

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

Start Hunting!