code with variables changes

Hi all
I have to write code for the following
Y_k = Ax_k + b
Z_k= Y_k +C
Now for K from 0 -200 the A = 2 and b=3,C =2
for K from 201- 400 the A = 4 and b=5, C=2
what code I can write to Specify these conditions? I mean chang in K, change the parameters values.

 Accepted Answer

If you have the Symbolic Toolbox, the easiest way can be to use piecewise()
Otherwise:
A = @(k) (k >= 0 & k<=200) .* 2*ones(size(k)) + (k>200 & k<=400) .* 4*ones(size(k));
b = @(k) (k >= 0 & k<=200) .* 3*ones(size(k)) + (k>200 & k<=400) .* 5*ones(size(k));
C = @(k) (k >= 0 & k<=400) .* 2*ones(size(k));
Y = @(k) A(k) + b(k);
Z = @(k) Y(k) + C(k);
fplot(Z, [-20 420])

4 Comments

thank you, but what about the ( X) why it is not mensioned ?
I thought Ax_k was all one variable name.
If you have an x_k but no formula for x_k then that implies that k is acting as a subscript. That is not impossible to deal with, but since k can be 0 then it implies that 0 is a valid subscript, which is not the case in MATLAB.
Y = @(k) A(k).*x(k) + b(k);
Z = @(k) Y(k) + C(k);
k = 1:numel(x);
plot(k, Z(k))
Thanks once again.
No, actaully it is Y= A*X_k+b
k is a subscript x1, x2 , x3 and so on. X is values only . we can start from x1
Then the code I gave should work.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!