I’m trying to use fsolve in matlab to solve a system of nonlinear equations numerically. Here is a test sample of my program, k1 and R are parameters and x0 is the start point.
function y=f(k1, R, x0)
pair=fsolve(@system,x0);
y=pair(1);
function r=system(v)
int1=@(x) exp(k1*x);
int2=@(x) exp(k1*x^2)/(x^4);
r(1)=exp(v(1))*quadl(int1,0,v(1));
r(2)=exp(k1*v(2))*quadl(int2,v(1),20)*k1*R;
end
end
The strange thing is when I run this program, matlab keeps telling me that I should use .^ instead of ^ in int2=@(x) exp(k1*x^2)/(x^4). I am confused because the x in that function handle is supposed to be a scalar when it is used by quadl. Why should I have to use .^ in this case?
Also I see that a lot of the examples provided in online documentations also use .^ even though they are clearly taking power of a scalar, as in here. Can anybody help explain why?
Thanks in advance.
in the function
int2you have used matrix power (^) where you should use element-wise power (.^). Also, you have used matrix right division (/) where you should use element-wise division (./). This is needed, sincequadl(and friends) will evaluate the integrandint2for a whole array ofx‘s at a time for reasons of efficiency.So, use this:
Also, have a look at
quadgkorintegral(if you’re on newer Matlab).By the way, I assume your real functions
int1andint2are different functions? Because these functions are of course trivial to solve analytically…