Reputation: 11
I want to solve a equation in MATLAB with fsolve
I'm trying to do that:
function F = root2d(P);
lambda = 2*10^-4;
th = -40:-1:-106;
PL1 = 10471285.480509; % (mw)
p1 = 10;
p2 = 6 ;
p3 = 8 ;
al = 2.5;
T = 10.^(th./10);
r = (p1*PL1^(-1)./T).^(1/al);
R = (p2*PL1^(-1)./T).^(1/al);
syms P
c = (lambda.*pi.*(R.^2));
j = 1:3;
D = zeros(3,67);
for k = 1:numel(j)
F(1) = (prod(D(k,:)==exp(P(1).*c.*(log(P(1)).^k/factorial(k))))).*exp(-(lambda*P(1).*pi.*(r.^2)))-P;
end
fun = @root2d;
P0 = 0;
P = fsolve(fun,P0)
Do you have an idea?
Upvotes: 1
Views: 144
Reputation: 3052
There are a couple of problems in this code. I will help you with the most tricky.
It is identified using rubberduck debugging. Let me exemplify it by explaining your code to you.
Starting from the top you define a function called root2d
, which returns some object F
, then in this function you define alot of constants. With these cosntants defined, you can define your symbolic function F
, which we earlier saw was the output of the function. Continuing in this function, root2d
, you now create a function handle fun
to root2d
, and then in the last line you call fsolve
to solve fun
.
Did you see the error?
fsolve
calls fun
which is a function handle to root2d
. Thus fsolve
calls root2d
. Running root2d
MATLAB comes across a new fsolve
which calls root2d
. I.e. root2d
will be pushed unto the stack over and over again.
This is solved by taking the last three lines out of the function, and then running them from another script or from the terminal.
Doing so results in a new error:
Unable to perform assignment because the indices on the left side are not compatible with the size of the right side.
Error in
sym/privsubsasgn
(line 1085)
L_tilde2 = builtin('subsasgn',L_tilde,struct('type','()','subs',{varargin}),R_tilde);
Error in
sym/subsasgn
(line 922)
C = privsubsasgn(L,R,inds{:});
Error in
SO
(line 18)
F(1) = (prod(D(k,:)==exp(P(1).*c.*(log(P(1)).^k/factorial(k))))).*exp(-(lambda*P(1).*pi.*(r.^2)))-P;
Error in
fsolve
(line 242)
fuser = feval(funfcn{3},x,varargin{:});
Caused by:
Failure in initial objective function evaluation. FSOLVE cannot continue.
I.e. you have to ensure that your objective function, F(1) =...
is correctly written.
Upvotes: 1