MATLAB: Integration from 0 to a variable.

integrationMATLAB

Hello everybody,
I have a problem with my integral, it is supposed to integrate the function from 0 to s and then equate that to RHS (which is an integer value previously obtained) to further obtain s.
Thank you all
%Left Hand Side of the Equation
fun2=@(x)((s-x).*((b/a).*((x/a).^(b-1)))/(1+((x/a).^b)).^2);
LHS_int=@(s)integral(fun2,0,s);
eqn_LHS=(-(p-c)).*s+((p-h)).*LHS_int==RHS;
sols=solve(eqn_LHS,s,'ReturnConditions',true);
disp(sols)
%This is the error I get.
Operator '.*' is not supported for operands of type 'function_handle'.

Best Answer

integral() is for numeric integration, but solve() is for symbolic work.
fun2=@(x)((s-x).*((b/a).*((x/a).^(b-1)))/(1+((x/a).^b)).^2);
That would capture the current value of s, a, and b, and record those in the function handle.
LHS_int=@(s)integral(fun2,0,s);
That would not use the s of @(s) inside fun2 -- not unless you pass it in to fun2.
fun2 = @(x,s) ((s-x).*((b/a).*((x/a).^(b-1)))/(1+((x/a).^b)).^2);
LHS_int = @(s)integral(@(x) fun2(x,s),0,s);
eqn_LHS = @(s) (-(p-c)).*s+((p-h)).*LHS_int(s) == RHS;
But then solve() does not work:
sols=solve(eqn_LHS,s,'ReturnConditions',true); %will fail
You can get one solution with, for example,
a = rand; b = rand; p = rand; c = rand; h = rand; RHS = randi([5 9]);
%the code
fun2 = @(x,s) ((s-x).*((b/a).*((x/a).^(b-1)))./(1+((x/a).^b)).^2);
LHS_int = @(s)integral(@(x) fun2(x,s),0,s);
eqn_LHS = @(s) (-(p-c)).*s+((p-h)).*LHS_int(s) - RHS;
x0 = 1;
[s, fval, exitflag] = fsolve(eqn_LHS, x0);
if exitflag < 0
fprintf('well, that did not work! Try again with better constants!\n');
else
disp(s)
disp(fval)
disp(exitflag)
end
But if you want the solution with Return Conditions and so on, you are going to need to switch to all symbolic. And it is rare that solve() of an integral equation works.