Equations

Sunday 12 June 2011

Adventures with wxMaxima

I was looking for a software that could help me with numeric as well as symbolic computations... something that could work like MATLAB and Mathematica, the two packages that I had some experience of using several years ago. I found the opensource wxMaxima which I downloaded from http://andrejv.github.com/wxmaxima/

The following are my experiments using the software.

Limits

Let's try finding out limits of a very simple function y=(2x+5) in the limit x-> -7.

(%i3) limit((2*x+5),x,-7);
(%o3) -9

So %i denotes the input line, %o denotes the output line. Semi-colon suppresses calculation. To calculate we need to press +. This is encouraging. Let us try our good old sin(x)/x in the limit x tending to 0.

(%i4) limit(sin(x)/x,x,0);
(%o4) 1

So let's look at the syntax...

limit(function, variable, value)

Incase we want a one sided limit i.e. approaching the value of x from left or right we can do the following:

(%i9) limit(2+sin(x)/x,x,-inf,minus);
(%o9) 2


Encouraged by the results I tried a few more options. Let us define a function in wxMaxima and then calculate the limit. Going back to the previous example:


(%i12) y:2+sin(x)/x;
limit(y,x,-inf,minus);
(%o12) sin(x)/x+2
(%o13) 2



Functions

We have seen above how to define functions. Can we have conditional functions as well? Let us say we want a function f(x) such that for x<2, f(x) = (3-x); for x=2, f(x)=2 and for x>2, f(x) = x/2. How do we implement this function in wxMaxima? This is done using blocks as follows.


(%i4) f(x):=block(if (x<2) then return (3-x),if(x=2) then return (2) else return (x/2));
(%o4) f(x):=block(if x<2 then return(3-x) ,if x=2 then return(2) else return(x/2))
(%i5) f(2);
(%o5) 2
(%i6) f(1.5);
(%o6) 1.5
(%i7) f(0.5);
(%o7) 2.5
(%i8) f(4);
(%o8) 2



Note that a colon is used to define a variable and impart a value to it. A function e.g. f(x) is created using := as above. To erase a variable assignment a command kill is used as follows:

kill(y)   kills the variable y
kill(all) kills all the variables


Differentiation 

Calculating differentials in wxMaxima is easy. Let's try a few.

(%i1) f(x):=sin(x);
diff(f(x),x);
(%o1) f(x):=sin(x)
(%o2) cos(x)



(%i3) y:x^(2/5);
diff(y,x);
(%o3) x^(2/5)
(%o4) 2/(5*x^(3/5))



(%i12) y:%e^x;
diff(y,x);
(%o12) %e^x
(%o13) %e^x



%e is wxMaxima's way of denoting the constant e.

Alright, looks good. Let us try evaluating higher order derivatives.

(%i16) y:3*x^2+2*x-5;
diff(y,x);
(%o16) 3*x^2+2*x-5
(%o17) 6*x+2
(%i18) diff(y,x,2);
(%o18) 6
(%i19) diff(y,x,3);
(%o19) 0


Partial derivatives?

(%i1) z:x^2+y^2;
diff(z,x);
(%o1) y^2+x^2
(%o2) 2*x
(%i3) diff(z,y);
(%o3) 2*y



Integration 


Integration in wxMaxima is as simple as all that we have seen above. Let's look at indefinite integrals first. What do I start with? Keeping it simple...

(%i5) y:x^2;
integrate(y,x);
(%o5) x^2
(%o6) x^3/3
(%i7) integrate(y,x,0,1);
(%o7) 1/3



What number exceeds its square by the largest amount?

(%i1) f(x):=x-x^2;
fp:diff(f(x),x);
(%o1) f(x):=x-x^2
(%o2) 1-2*x
(%i3) solve(fp);
(%o3) [x=1/2]



What number is exceeded by its square root by the largest amount?


(%i1) f(x):=x-sqrt(x);
diff(f(x),x);
(%o1) f(x):=x-sqrt(x)
(%o2) 1-1/(2*sqrt(x))
(%i3) solve(diff(f(x),x));
(%o3) [x=1/4]



Find two positive numbers whose sum is 50 and whose product is as large as possible.


(%i7) prod:a*(50-a);
(%o7) (50-a)*a
(%i8) diff(prod,a);
(%o8) 50-2*a
(%i9) solve(diff(prod,a));
(%o9) [a=25]


Find two positive numbers x and y whose sum is 30 and are such that xy2 is as large as possible.


(%i1) assume(x>0,y>0);
y:30-x;
p:x*y^2;
(%o1) [x>0,y>0]
(%o2) 30-x
(%o3) (30-x)^2*x
(%i4) s:diff(p,x);
(%o4) (30-x)^2-2*(30-x)*x
(%i5) solve(s);
(%o5) [x=10,x=30]



A store has been selling a popular computer game at the price of $40 per unit, and at this price, players have been buying 50 units per month. The owner of the store wishes to raise the price of the game and estimates that for each $1 increase in price, three fewer units will be sold each month. If each unit costs the store $25, at what price should the game be sold to maximize profit?

(%i1) sp0:40;
n:50;
cp:25;
unitprofit:sp0-cp;
(%o1) 40
(%o2) 50
(%o3) 25
(%o4) 15
(%i5) profit:unitprofit*n;
(%o5) 750
(%i6) sp1:sp0+x;
np:n-3*x;
nuprofit:sp1-cp;
nprofit:nuprofit*np;
(%o6) x+40
(%o7) 50-3*x
(%o8) x+15
(%o9) (50-3*x)*(x+15)
(%i10) solve(diff(nprofit,x));
(%o10) [x=5/6]



The price of the computer game should go up by $0.83 to maximize the profits.


A Florida citrus grower estimates that if 60 orange trees are planted, the average yield per tree will be 400 oranges. The average yield will decrease by 4 oranges per tree for each additional tree planted on the same acreage. How many trees should the grower plant to maximize the total yield?



(%i14) ntrees:60;
pertree:400;
dpertree:-4*x;
nntrees:60+x;
yield:nntrees*(pertree+dpertree);
(%o14) 60
(%o15) 400
(%o16) -4*x
(%o17) x+60
(%o18) (400-4*x)*(x+60)
(%i19) solve(diff(yield,x));
(%o19) [x=20]


Therefore, the farmer should plant 20 more trees i.e. he should have 80 trees on his farm.

Suppose $1,000 is invested at an annual interest rate of 6%. Compute the balance after 10 years if the interest is compounded
a. Quarterly b. Monthly c. Daily d. Continuously




Quarterly implies four periods in a year, forty periods in ten years.
 
(%i30) principal:1000;
rate:0.06;
time:10;
periods:4;
amount:principal*(1+rate/periods)^(periods*time);
(%o30) 1000
(%o31) 0.06
(%o32) 10
(%o33) 4
(%o34) 1814.018408668941


Monthly means 120 periods.


(%i35) principal:1000;
rate:0.06;
time:10;
periods:12;
amount:principal*(1+rate/periods)^(periods*time);
(%o35) 1000
(%o36) 0.06
(%o37) 10
(%o38) 12
(%o39) 1819.39673403228



Daily means 365 periods in a year.


(%i40) principal:1000;
rate:0.06;
time:10;
periods:365;
amount:principal*(1+rate/periods)^(periods*time);
(%o40) 1000
(%o41) 0.06
(%o42) 10
(%o43) 365
(%o44) 1822.028954538675



Continuously implies an exponential increase


(%i45) principal:1000;
rate:0.06;
time:10;
amount:principal*%e^(rate*time);
(%o45) 1000
(%o46) 0.06
(%o47) 10
(%o48) 1822.118800390509

No comments:

Post a Comment