Subroutine using if statement

What I'm trying to do is to supply different value of constant k at different x intervals. However, it seems that my y(x==3) is not equal to y_actual.
x=0:20;
k=X(x); %Subroutine func
y=k*x;
y_actual = (5+2)*3 % what i wanted
y(x==3)
function k=X(x)
if (x>=2)
w=5;
else
w=4;
end
k=w+2;
end

2 Comments

when u send X(x) which already conatins values <2, so the if will always go to the else (w=4)
I see. So I shouldn't pass x into the function X?

Sign in to comment.

 Accepted Answer

I generally favour this sort of approach to such prolblems —
X = @(x) ((x>=2).*5 + (x<2).*4) + 2;
x = 0:20;
k = X(x);
y = k.*x;
figure
plot(x, y, '+-')
grid
xy = table(x(:),y(:), 'VariableNames',{'x','y'})
xy = 21×2 table
x y __ ___ 0 0 1 6 2 14 3 21 4 28 5 35 6 42 7 49 8 56 9 63 10 70 11 77 12 84 13 91 14 98 15 105
.

7 Comments

Thank you so much! My actual assignment is much more complex with 2d heat equations, so i make this simple question trying to understand the concept. Anyways, thank you once again.
Anna Lin
Anna Lin on 13 Jun 2021
Edited: Anna Lin on 13 Jun 2021
what if I have another y interval to be considered? like a box area when (x<=a & y<=b)?
As always, my pleasure!
To include another condition, simply add it (providing the other conditions do not overlap oir conflict with existing conditions):
X = @(x) ((x>=2).*5 + (x<2).*4) + 2 + ((x<=a) & (y<=b)).*something
However, ‘X’ then needs to be a function of both ‘x’ and ‘y’ with appropriate additional constants and arguments, as necessary. It will be necessary to experiment with to get the desired result.
This approach generally works for most such situations, however more complicated situations may require additional such functions, or a completely different approach altogether.
.
Alright, I'll work around with it. Thank you very much!
The image was not present earlier.
Example —
F = @(x,y) ((x<=3) & (y<=5)).*(-2) + ((x>=20) & (y<=5)).*2;
xv = linspace(0,40,40);
yv = linspace(0,10,10);
[Xm,Ym] = ndgrid(xv,yv);
Zm = F(Xm,Ym);
figure
surfc(Xm, Ym, Zm)
grid on
axis('equal')
.
Thank you very very much!
As always, my pleasure!

Sign in to comment.

More Answers (1)

Lots of stuff wrong with this. For starters, you're passing in the whole x vector into the (poorly-named) X function yet your function seems to be expecting only a single value of x, not a whole multi-element vector. Take the semicolons off the ends of the lines and you'll see exactly what it's doing, which is exactly what you told it. You said x==3 which gives a 21 element logical vector with false everywhere except at index 4 (where x has the value 3) and it's true there.
ans =
1×21 logical array
0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
So then y(4) = 18 which is why it's giving you 18.
I'm not sure if you expect the k to be a vector or a scalar so I'm not sure how to tell you to fix it.
x=0:20
k=X(x) % Subroutine function
y=k*x
y_actual = (5+2)*3 % what i wanted
y(x==3)
function k=X(x)
if (x>=2)
w=5;
else
w=4;
end
k=w+2;
end
x =
Columns 1 through 20
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
Column 21
20
k =
6
y =
Columns 1 through 20
0 6 12 18 24 30 36 42 48 54 60 66 72 78 84 90 96 102 108 114
Column 21
120
y_actual =
21
ans =
18

1 Comment

Anna Lin
Anna Lin on 13 Jun 2021
Edited: Anna Lin on 13 Jun 2021
The k should be a vector.

Sign in to comment.

Categories

Products

Release

R2021a

Community Treasure Hunt

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

Start Hunting!