Ask Your Question
0

Substitute and evaluate

asked 2014-04-16 06:47:55 +0200

Blackadder gravatar image

Hello, I would like to ask a really easy question:

How can I substitute a variable into an equation and have it evaluated simultaneously? If I have $f = a + b$ and would it to return $5$ if $a=2$ and $b=3$, how shall I proceed?

I've tried the following

a, b = var('a, b');
f = a + b;
a = 2; b = 3; f

But it returned a+b, which is not what I had in mind. Thanks for the help in advance!

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2014-04-16 09:44:14 +0200

Antlab gravatar image

You can use:

f (x,y)=x+y

a = 2; b = 3

f(a,b)

In this way you obtain 5, the desired result. Actually you don't need x and y, you could use only a and b, but I put different names just to underline the mechanism. The first line defines the function f with generic variables. The last line calculates the value of f when you actually pass the values of the variables. It can be a bit confusing at first, I hope my example can help.

edit flag offensive delete link more

Comments

Thanks Antlab!

Blackadder gravatar imageBlackadder ( 2014-04-17 00:04:40 +0200 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2014-04-16 06:47:55 +0200

Seen: 476 times

Last updated: Apr 16 '14