Loading [MathJax]/jax/output/HTML-CSS/jax.js
Ask Your Question
0

Substitute and evaluate

asked 10 years ago

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!

Preview: (hide)

1 Answer

Sort by » oldest newest most voted
1

answered 10 years ago

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.

Preview: (hide)
link

Comments

Thanks Antlab!

Blackadder gravatar imageBlackadder ( 10 years ago )

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: 10 years ago

Seen: 653 times

Last updated: Apr 16 '14