Failure in initial user-supplied objective function evaluation. FSOLVE cannot continue - matlab

I am trying to solve a large set of non-linear equation using fsolve in MATLAB 2013. When I run this code:
Unkn0=zeros(3249,1); % Guess
[Unkn,feval,exitflag,output]=fsolve(Eqnh,Unkn0);
Here Eqnh is a function handle for 3249 variables and 3249 equations.
I get the following error:
Error using
Not enough input arguments.
Error in fsolve (line 218)
fuser = feval(funfcn{3},x,varargin{:});
Caused by:
Failure in initial user-supplied objective function evaluation. FSOLVE cannot continue.
I can provide Eqnh written in a text file if there is any need of it.

Related

solving a quadratic equation with matrices in matlab

I'm trying to numerically find the solution to X^2+X+C=0 where C is the matrix C=[-6,-5;0,-6] and 0=[0,0;0,0], a quadratic equation where the variable is 2x2 matrix.
So I wrote the following matlab commands
C=[-6,-5;0,-6]
[X1,F,e_flag]=fsolve('X^2+X+C',[1,1;1,1])
where [1,1;1,1] is our initial estimate, or X0.
I'm getting the following errors
"Error using inlineeval (line 15)
Error in inline expression ==> X^2+X+C
Undefined function or variable 'X'.
Error in inline/feval (line 34)
INLINE_OUT_ = inlineeval(INLINE_INPUTS_, INLINE_OBJ_.inputExpr,
INLINE_OBJ_.expr);
Error in fsolve (line 218)
fuser = feval(funfcn{3},x,varargin{:});
Caused by:
Failure in initial user-supplied objective function evaluation. FSOLVE
cannot continue."
How do I use fsolve to solve these kind of problems?
I don't think fsolve can be used with a string representation of your equation.
You should rather pass a function handle to the solver like this:
C = [-6,-5;0,-6];
[X1,F,e_flag] = fsolve(#(X) X^2+X+C,[1,1;1,1]);
It also depends on what you mean by: X^2. In Matlab this means the matrix product X*X. If you want entry-wise squaring you should use X.^2, in which case you will have four independent quadratic equations, which you could solve independently.

Solving an ode numerically in matlab

I understand the logic of the numerical solving, that matlab starts with a value for your unknown, and loops until the equation converges to a value. However what I don't understand is the proper way to enter my equation. I think that using the ode45 function is the best way to do this. I have the following equation U^(n+1) = U^n - (t'*3250/10)-(t'/2)(.004(v^n)^2/10.
I have a suspicion my equation needs to be in a different form however I am unsure of the correct way to have matlab solve the equation.
I tried entering the equation in matlab as is, however it complains that v and n are unknown variables and I am unsure of how to handle those. The final goal of solving this equation is to find the value for v.
C=#(t,v) u^n-(3250*t'/10)-(t'/2)*((.004*(v^n)^2)/10)
[t,v]=ode45(C,[0,5],1)
produces the following errors:
Undefined function or variable 'u'.
Error in #(t,v)u^n-(3250*t'/10)-(t'/2)*((.004*(u^n)^2)/10)
Error in odearguments (line 88)
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode45 (line 114)
{neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs, odeFcn, ...
Error in ae301_3 (line 2)
[t,v]=ode45(C,[0,5],1)
ode solvers are used of Ordinary Differential Equations. Your equation is not differential but it is a non linear algebraic equation.
In Matlab you can solve it using fzero. However your function can be easily rearranged to be explicit and can be solved analytically without any iterative procedure.

Non-linear equation MATLAB

I have no idea how I could solve this equation with matlab:
f(1)=0.098253*x(1)-atan((tan(x(1))-tan(x(2)))/2)*0.531268-0.433015*x(2)-0.27994
f(2)=0.9951*x(1)-atan((tan(x(1))-tan(x(2)))/2)*0.12909+0.866022*x(2)-0.350005;
I tryed with function = f and then [x,eval,flag]=fsolve('ecuaciones',x0); but I have an error:
Error in ==> fsolve at 254
fuser = feval(funfcn{3},x,varargin{:});
Caused by:
Failure in initial user-supplied objective function evaluation. FSOLVE
cannot continue.
And Matlab says to me that is double type... but I have no idea how I can resolve!! I am new using Matlab, for that reason I need the answer clearly!!
THANK YOU!
I dont think your equations can be solved for anything you've only one degree of freedom because f(2) is a multiple of f(1)

fit with lsqcurvefit does not work maybe function maldefined

I am trying to fit function F to experimental data.
x_tem and yd are both vectors of size (12,1). The function should find the best
fitting value y_tau of the function to the experimental data.
I just can't find the mistake - matlab is showing me the error :
Error in lsqcurvefit (line 199)
initVals.F = feval(funfcn_x_xdata{3},xCurrent,XDATA,varargin{:});
Caused by:
Failure in initial user-supplied objective function evaluation. LSQCURVEFIT cannot continue.
The code is:
x_tem=Temp_aero_korrektur(:,1);
yd=Temp_aero_korrektur(:,2);
F = #(y_tau,x_tem)((-1)*((273.15-x_tem)*(273.15-y_tau(1))*8.314* (((17.62*x_tem)/(243.12+x_tem))-((17.62*y_tau(1))/(243.12+y_tau(1)))))/(40714.53));
yd_tau = lsqcurvefit(F,-40,x_tem,yd);
There are two possibilities here. One is that you do actually want to use matrix operations in your objective function (so that, for example, x_tem/x_tem gives a single scalar value using mrdivide). If this is the case then you should be calling lsqcurvefit with the transpose of x_tem
yd_tau = lsqcurvefit(F,-40,x_tem',yd);
The other option is that you actually meant to calculate your objective function on each value of x_tem (so, for example, using x_tem./x_tem to give a vector the same length as x_tem). If this is the case then your objective function should be
F = #(y_tau,x_tem)((-1)*((273.15-x_tem).*(273.15-y_tau(1)).*8.314.* (((17.62*x_tem)./(243.12+x_tem))-((17.62*y_tau(1))/(243.12+y_tau(1)))))/(40714.53))
(See documentation for times and rdivide for element-wise operations)

Matlab fitting data to a function, where the function is a summation and periodic, syntax

I am attempting to fit temperature data over the last 50 years for the US using the lsqcurvefit, but I am concerned that my fitting function has bad syntax.
The fitting function itself is of the form:
∑(ai+bi*t+ci*t^2)*(cos(vt+p))
Summing from i=1 to N. I would like t to correspond to the time vector called TIME. Ideally the fitting should return values for a, b, c, v, and p which describe the temperature data
I am attempting to fit daily maximum temperatures, a vector called TMAX.
So far I have:
lsqcurvefit(fitFn(1,2,TIME),5,TIME,TMAX)
Where fitFn is defined in a script as
function f = fitFn(i,N,t)
f=0;
for i=1:N
f =#(i,N,a,b,c,v,p,t) f + (a+b*t+c*t^2)*(cos(v*t+p));
end
However whenever I run lsqcurvefit I get the error
Error using
fitFn>#(i,N,a,b,c,v,p,t)f+(a+b*t+c*t^2)*(cos(v*t+p)) (line
4)
Not enough input arguments.
Error in lsqcurvefit (line 199)
initVals.F =
feval(funfcn_x_xdata{3},xCurrent,XDATA,varargin{:});
Caused by:
Failure in initial user-supplied objective function
evaluation. LSQCURVEFIT cannot continue.
I am sure I'm making a simple error but I'm somewhat at a loss. I'm new to MatLab and don't quite understand how to use anonymous functions. Any help would be greatly appreciated.
Thanks for reading
Your fitFn is very wrong. I think this is what you want:
f = #(x,t) (x(1)+x(2)*t+x(3)*t.^2)*cos(x(4)*t+x(5))
lsqcurvefit(f,zeros(1,5),TIME,TMAX)