Azzera filtri
Azzera filtri

if statement inside of a Function is working only in certain conditions

2 visualizzazioni (ultimi 30 giorni)
Hi everyone, I'm expecting the function capacitorVoltage(t) to return '5' when the value of 't' is less than zero. However, as shown below it does not give the value '5'. Any help is greatly appreciated.
Edit: When trying only values where (t<=0) the function returns the expected output. However, when the range of "t" varies from negative to postive values it does not return the value '5' for values of 't' less than zero
%% V_c function defined at the end of file
t=-2:0.2:5
t = 1×36
-2.0000 -1.8000 -1.6000 -1.4000 -1.2000 -1.0000 -0.8000 -0.6000 -0.4000 -0.2000 0 0.2000 0.4000 0.6000 0.8000 1.0000 1.2000 1.4000 1.6000 1.8000 2.0000 2.2000 2.4000 2.6000 2.8000 3.0000 3.2000 3.4000 3.6000 3.8000
v_c=capacitorVoltage(t)
mask1 = 1×36 logical array
1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
v_c = 1×36
44.1387 34.7071 27.3617 21.6411 17.1859 13.7162 11.0140 8.9095 7.2705 5.9941 5.0000 4.2258 3.6229 3.1533 2.7876 2.5028 2.2810 2.1082 1.9737 1.8689 1.7873 1.7237 1.6743 1.6357 1.6057 1.5823 1.5641 1.5499 1.5389 1.5303
function[x]= capacitorVoltage (t)
mask1= t<0
mask2= t>=0;
x=1.5+3.5*exp(-1.25.*t);
if (mask1==1)
x=5;
end
end

Risposta accettata

John D'Errico
John D'Errico il 12 Feb 2023
Modificato: John D'Errico il 12 Feb 2023
You don't understand how an if statement works on a vector. For example, what happens when I do this? Surely, in terms of the code you wrote, you would expect to see the vector [3 3 3 4 5] as a result?
x = 1:5;
if x < 3
x = 3;
end
x
x = 1×5
1 2 3 4 5
So why did that not work? It failed, because if statements don't work that way!
An if statement is not a loop, that tests each element in the if, and depending on which of those elements satisfy the if, it applies to them. That is what I think you expect. In fact, this is a common mistake made.
The if statement, as applied to a conditional, where the result of that conditional test is a vector, only executes if ALL of those elements return true. For example, try this:
if x < 6
x = 3;
end
x
x = 3
Why did that happen? What does the test here return?
x = 1:5;
x < 6
ans = 1×5 logical array
1 1 1 1 1
So if x is the vector 1:5, then the test is true for every elememnt. An the if statement executes. But then what did it do? Inside the if clause, it set the variable x to the number 3. The if statement does not assign only some of those elements the value 3. It turns the vector into a scalar, by assigning 3 to x.
This means the code you wrote cannot possibly work as you wish.
  3 Commenti
John D'Errico
John D'Errico il 12 Feb 2023
Your mistake is actually pretty common, in thinking that an if statement would work like a loop. So much so that I wonder if maybe the documentation and the tutorials should stress that an if does not work that way.
Julian
Julian il 12 Feb 2023
I was previuosly exposed to the principle of 'vectorization' through some warnings and errors that appeared in my code, but I hadn't grasped the idea or purpose of it. Naively, my only other background is C++ and I figured that functions in MATLAB would take my vector 't' and plug it into the function element-by-element since that is the only way that I know how to input a range of values into a function. Clearly, MATLAB doesn't revolve around me and C++ code wouldn't work this way either. But, thank you again for the thorough introduction.

Accedi per commentare.

Più risposte (3)

the cyclist
the cyclist il 12 Feb 2023
Modificato: the cyclist il 12 Feb 2023
This statement
if (mask1==1)
checks if all elements of mask1 are equal to 1, and executes the next line once if they are. It is equivalent to
if all(mask1==1)
A vectorized way to do what you want (I think), without the if statement, is
x(mask1==1) = 5;

Star Strider
Star Strider il 12 Feb 2023
I would just set this up as an anonymous function —
capacitorVoltage = @(t) 5*(t<0);
t=-2:0.2:5;
v_c=capacitorVoltage(t);
Result = [t; v_c]
Result = 2×36
-2.0000 -1.8000 -1.6000 -1.4000 -1.2000 -1.0000 -0.8000 -0.6000 -0.4000 -0.2000 0 0.2000 0.4000 0.6000 0.8000 1.0000 1.2000 1.4000 1.6000 1.8000 2.0000 2.2000 2.4000 2.6000 2.8000 3.0000 3.2000 3.4000 3.6000 3.8000 5.0000 5.0000 5.0000 5.0000 5.0000 5.0000 5.0000 5.0000 5.0000 5.0000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
figure
plot(t, v_c)
grid
xlabel('t')
ylabel('v_c')
axis([-3 6 -0.1 5.1])
.

Askic V
Askic V il 12 Feb 2023
Modificato: Askic V il 12 Feb 2023
I would implement the function in a bit different way:
t = -2:0.2:5;
v_c = capacitorVoltage(t);
plot(t, v_c);
xlabel('t'); ylabel('v\_c');
function x = capacitorVoltage (t)
x = 1.5+3.5*exp(-1.25.*t);
x(t<0) = 5;
end
  8 Commenti
Julian
Julian il 12 Feb 2023
On second hand, it seems like this may be the perfect way to check a condition regarding 't'. Mainly because 't' comes into the function as a vector and it will do multiplication across anything we choose to multiply it with. We just have to add the logical operators to each expression to filter out what we do not want.
Julian
Julian il 12 Feb 2023
Just saw your reply after I sent my previous message. Yes, you are correct that caution should be used in these if/else statements if checking the contents of a vector.

Accedi per commentare.

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by