Solving an ode numerically in matlab - 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.

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?

Using Matlab to solve a system of ODEs using Euler's method

I have created a function Euler.m to solve a a system of ODEs using Euler's method. I wish to use this function to solve the system of ODEs defined by the anonymous function func=#(t) ([x(t)+4*y(t)-exp(t);x(t)+y(t)+2*exp(t)]) with initial conditions given by y0.
func=#(t) ([x(t)+4*y(t)-exp(t);x(t)+y(t)+2*exp(t)]);
y0=[4;5/4];
y_exact=#(t) [4*exp(3*t)+2*exp(-t)-2*exp(t);2*exp(3*t)-exp(-t)+exp(t)/4]; %exact solution of ODEs
a=0; % such that
b=1; % a<t<b
N=120;
[t,y] = Euler(func,a,b,y0,N)
However, the following error is displayed:
"Error using solution>#(t)([x(t)+4*y(t)-exp(t);x(t)+y(t)+2*exp(t)])
Too many input arguments.
Error in solution (line 7)
[t,y] = Euler(func,a,b,y0,N)".
Why is this error being displayed?
You are pretending that you already know when writing the ODE function func what the solutions x(t),y(t) are. Then you are going to compute solutions approximations for it. This is completely the wrong way around.
The function for the right side is just for a point in phase space, so you need
func=#(t,y) ([y(1)+4*y(2)-exp(t);y(1)+y(2)+2*exp(t)]);
where the input y is a two-component vector.

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.

ode45 Not Enough Initial Conditions

I have defined a function using matlabFunction. Here is the code:
matlabFunction([a16;-((1+x16^2)/(2*x16))*a16],'vars',{x16,[a16]},'file','DE_19')
which seemed to work. However, when I try to use ode45 to solve the differential equation defined by matlabFunction, I get an error. Here is the code:
[x,y] = ode45(#(x16,Y) DE_19(x16,Y),[1,11],[2,7,5]);
The error I get is
Error using odearguments (line 93)
#(X16,Y)DE_19(X16,Y) returns a vector
of length 6, but the length of
initial conditions vector is 3. The
vector returned by
#(X16,Y)DE_19(X16,Y) and the initial
conditions vector must have the same
number of elements.
Error in ode45 (line 114)
[neq, tspan, ntspan, next, t0,
tfinal, tdir, y0, f0, odeArgs,
odeFcn, ...
So, I tried changing my initial conditions from [2,7,5] to [2,7,5,8,9,4]. When I did this, I got the same message, but instead of saying that the vector returned is length 6, and that the length of my initial condition vector was 3, it said that the vector returned was of length 12, and that the length of my initial condition vector was 6.
Why is it doing this? This seems strange that the length of the vector returned would vary as I vary the length of the initial condition vector.
Have you looked at the contents of DE_19.m? You should provide the code you used before calling matlabFunction, but here's a runnable version that might be something like what you used:
syms a16 x16;
DE_19 = matlabFunction([a16;-((1+x16^2)/(2*x16))*a16],'vars',{x16,[a16]})
This returns:
DE_19 =
#(x16,a16)[a16;(a16.*(x16.^2+1.0).*(-1.0./2.0))./x16]
As you can see, if you pass in a scalar state (the second argument, a16, is the state and the first, x16 is the independent variable – this is true for all ODE solvers: t then y), the output will always two elements. And indeed the output will always be twice as long as the input state. Maybe a16 and x16 should be switched? Look at the help and documentation for matlabFunction as they provide an example that does exactly this.
By the way, there's no real need to create a file. You can use the anonymous function returned by matlabFunction like this (you'll need to figure out which variable is which and what is missing to still get it to work):
DE_19 = matlabFunction([...],'vars',{...})
[x,y] = ode45(DE_19,[1,11],[2,7,5]);

singularity at differential equation with MATLAB

I can't solve this differential equation by ode45 beacause it has sigularity.
xy"=3xcos(x)+sin(x) ; x(0)=0 , x'(0)=0
can you help me to write ode45 function?
You can use the sinc(x) function, which is defined as sin(π*x)/(π*x), except at x=0 where its value is 1. So, you can rewrite your ODE as:
y'' = 3*cos(x) + sinc(x/π)
which ode45 shouldn't have any trouble solving.