solving a quadratic equation with matrices in matlab - 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.

Related

solving nonlinear equations including max function

I want to solve this system of equations using MATLAB:
I solved the second equation and put it in the first one. Then I tried this:
syms v_star
eqn = max((1.8+0.81*v_star), (-1+0.9*v_star)) == v_star;
solx = solve(eqn,v_star)
And I get this error:
Error using symengine
Input arguments must be convertible to floating-point numbers.
How can I solve this system of equations?

MATLAB error when solving simultaneous equations

I am running a MATLAB code which solves a set of non-linear simultaneous equations. The code can be found here. The data input for the code (in excel) can be found here.
I encounter the following error:
Error using sym/subsasgn (line 733)
Indexed assignment to empty SYM objects is supported only in the 0-by-0 case.
Error in Solution (line 69)
x(i,:) = (b(i,1) - b0)./(c(i,1)*x0) + c0/c(i,1);
Does anyone have any idea how I can resolve this?
When declaring a symbolic variable, you have to specify the dimensions, otherwise it's a scalar. Instead of syms x; use sym and set the dimension argument:
x=sym('x',[3,4])
Replace [3,4] with the dimensions you want.

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)

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

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.