MATLAB: How to use fsolve with a system of 2 nonlinear equations in a for that changes a parameter at each iteration

fsolvefsolve loopMATLABnonlinear

Hi everyone, thanks in advance for any help or tip you will give me.
I have a problem about solving a system of 2 nonlinear equation of 2 variables and 1 parameters, the one which would change during every iteraction (I tried to search in other questions opened before mine but it seems i couldn't find anything relatable or solvable for my case).
The problem at the beginning of my matlab code is to find self-consistent solutions of a a system of 2 nonlin. equation in 2 variables, but one of these equations with a parameter that is the components of a given vector V. The result i need is a matrix with the 2 solutions in the rows, and column based on V dimension.
My thought was to use this kind of code, but i dont understand the source of the error "Unable to perform assignment because dot indexing is not supported for variables of this type.".
Am i just using a bad code, or what i need is not solvable by fsolve, and i need another approach?
(Sorry for any mistake, english in not my mother tongue)
Code:
function F(Cq,Vch,V) %%variables are Cq and Vch, V should be the parameter
%%...
%%constants definitions
%%..
F(1)= Cq-(K*kB*T)*log(2*(1+cosh((q*Vch))/(kB*T))));
F(2)= Vch -(Vgstop-V)*(Cox/(Cox+0.5*Cq));
end
%%different file
%...

% constant definitions
%...
V=-1:0.01:1; %%vector of parameters
Cq0=0; %initial guess

Vch0=0; %initial guess
for i=1:length(V)
iterfun=@(Cq,Vch) F(Cq,Vch,V(i)});
ans(i,:)=fsolve(iterfun,Cq0,Vch0);
end

Best Answer

  • Note: Lots of parameters were not defined so please take care of it.
    %%different file
    %...

    % constant definitions
    %...
    V=-1:0.01:1; %%vector of parameters
    Cq0=0; %initial guess

    Vch0=0; %initial guess
    Roots=cell(1,numel(V)); % preallocate!
    for i=1:length(V)
    iterfun=@(x)Fdd(x,V(i));
    Roots{i}=fsolve(iterfun,[Cq0;Vch0]); % don't name variable as ans
    end
    celldisp(Roots)
    Roots= [Roots{:}] % double array
    function F=Fdd(x,V) %%variables are Cq and Vch, V should be the parameter
    %%...
    %%constants definitions
    %%..
    Cq=x(1);
    Vch=x(2);
    K=6; % your value




    kB=8; % your value
    q=10; % your value
    T=2; % your value
    Vgstop=3; % your value
    Cox=4; % your value
    F(1)= Cq-(K.*kB.*T).*log(2.*(1+cosh((q.*Vch))./(kB.*T)));
    F(2)= Vch -(Vgstop-V).*(Cox./(Cox+0.5.*Cq));
    end