%problem: given a function f(x)
%find b s.t. the average value of f on [0,b] is 7.

%THE WAY OF LIGHTweight computation, a.k.a. numerically:

f = @(x) exp(x.^3); %defines a function of x
%Now, define ave(b) = average of f on [0,b] as a function:
ave = @(b) integral(f, 0, b)/(b - 0); 
%We look for b s.t. ave(b) = 7 --> ave(b)-7 = 0.
%Define a goal function: goal(b) = ave(b) - 7 
%and look for its zeros:
goal = @(b) ave(b) - 7;
%try some numbers:
try_1 = goal(1)
try_2 = goal(2)
%zero is between 1 and 2, so 1.5 is a good initial guess/
%Find a zero, and time the execution:
B = fzero(goal, 1.5)
%We get an answer. Let's verify that it is correct:
average = integral(f,0,B)/B
%The average is indeed what we want.

%Let's repeat the computation and time it:
tic; %START THE TIMER
f = @(x) exp(x.^3); 
ave = @(b) integral(f, 0, b)/(b - 0); 
goal = @(b) ave(b) - 7;
B = fzero(goal, 1.5);
time1 = toc %STOP THE TIMER and store elapsed time


%Now, the same computation DONE SLOOOOOOOOOOWLY:
clear x b f ave B;
syms x b
tic; %START THE TIMER
f(x) = exp(x^3);
ave(b) = int(f,x,0,b);
B = solve(ave(b) == 7)
time2 = toc %STOP THE TIMER

%How much slower this is compared to doing it 
%numerically from the start? This many times:
symbolics_are_this_many_times_slower = time2/time1

%..and this is by far not the worse. 
%Try changing the code above to f(x)=exp(x^3)*x

%See if you have enough patience for this to finish.

%On top of it, the symbolic toolkit fails, and still does it
%numerically internally (you might get a warning)!

%There is a place for symbolic computation: for instance,
%when your output is a rational number a/b, and you
%need the INTEGERS a and b. Or when you would prefer
%an expression like sqrt(2) to 1.41. But in many contexts,
%symbolic computation is going to be very slow.