Complicated Nonlinear system Numerical differentiating - matlab

I have a nonlinear steady space matrix.I need to solve the differential as shown in the pic below:
I explained more on pic(Here dD/dx)

You can do symbolic differentation with the MATLAB Symbolic Toolbox, but you have to tell it to perform the right partial derivatives. Your dependent variable (x) has three components, so you cannot take the partial derivative of D with respect to x, but you can take the partial derivative with respect to x1, x2, and x3.
syms t x1 x2 x3 x1_dot x2_dot x3_dot
x1_dot = diff(x1, t)
x2_dot = diff(x2, t)
x3_dot = diff(x3, t)
D(x1,x2,x3) = [... your function matrix ... ]
dD_x1 = diff(D,x1)
dD_x2 = diff(D,x2)
dD_x3 = diff(D,x3)

Related

How do I make this continuous regression?

Given the function f(x) = ln(x) with x belonging to [1,e] You must develop an algorithm in matlab that performs the following tasks.
Perform the continuous regression of f(x) onto the subspace 〈1, x, x^2〉, using the symbolic tools in Matlab. Plot the result (the function and the fitting).
Perform the discrete regression of f onto 〈1, x, x^2〉. For that purpose:
2.1. Introduce as regular sampler of the interval [1, e] using 1000 points. Call this sampler xs.
2.2. Evaluate f(x) on xs, and the basis functions 1, x, x^2 to generate {1, x, x^2, f}.
2.3. Perform the regression of f onto 〈1, x, x^2〉.
2.4. Plot the result (the function and the fitting)
i tried with this but it is not giving the right answer, and it is giving an error in plot(xs, f_values)
clear all
close all
clc
syms x
f = log(x);
V = [1 x x^2];
coeffs = V\f;
a0 = coeffs(1);
a1 = coeffs(2);
a2 = coeffs(3);
fitting = a0*1+ a1*x + a2*x^2;
ezplot(f,[1,exp(1)])
hold on
ezplot(fitting,[1,exp(1)])
legend('f(x)','fitting')
xs = linspace(1,exp(1),1000);
linspace(1,exp(1),1000)
f_values = subs(f, x, xs);
basis_1 = ones(1, 1000);
basis_x = xs;
basis_x2 = xs.^2;
V = [basis_1; basis_x; basis_x2];
fitting_values = coeffs(1)*basis_1 + coeffs(2)*basis_x + coeffs(3)*basis_x2;
plot(xs, f_values)
hold on
plot(xs, fitting_values)
legend('f(x)', 'fitting')
Looking solely at your error in plot(xs, f_values):
MATLAB's page for the function plot shows that when accepting two inputs (in your case, xs and f_values), they are either (x, y), or (y, line options). But since one of the values you've inputted are based on x (a symbolic value), it's a symbolic vector. plot can't handle symbolic inputs.
I believe the equivalent for syms is fplot from the symbolic maths toolbox.

How to solve a coupled nonlinear first order differential equation?

I have a problem in the form of 2 equations and known initial conditions. The equations are:
dx/dt = (-a1*sin(y) + a2 + a3*sin(y-x)) / ((dy/dt)*a4*cos(y-x))
dy/dt = (a1*sin(x) -a5 + a6*x + a7*sin(y-x)) / ((dx/dt)*a8*cos(y-x))
where a1 to a8 are variables.
I am trying to plot x vs t and y vs t on MATLAB, but I am not sure how to solve this numerically or analytically. Any help would be appreciated!!
The ODE of your problem cannot be written as dy/dt=f(t,y) nor M(t,y)dy/dt=f(t,y). That means it is a Differential Algebraic Equation which has to be solved numerically in the form:
f(t, y, dy/dt)=0
In matlab this can be done with the command ode15i.
Therefore, the first step is to write the function in a proper way, in this case, one option is:
function f = cp_ode(t,y,yp,a)
f1 = (-a(1)*sin(y(2))+a(2)+a(3)*sin(y(2)-y(1)))/yp(2)*a(4)*cos(y(2)-y(1)) - yp(1);
f2 = (a(1)*sin(y(1))-a(5)+a(6)*y(1)+a(7)*sin(y(2)-y(1)))/yp(1)*a(8)*cos(y(2)-y(1)) - yp(2);
f = [f1 ; f2] ;
end
Then, the initial conditions and integration timespan can be set in order to call ode15i:
tspan = [0 10];
y0 = [1; 1] ;
yp0 = fsolve(#(yp)cp_ode(0,y0,yp,a),<yp0_guess>);
a = ones(1,8) ;
[t,y] = ode15i(#(t,y,yp)cp_ode(t,y,yp,a), tspan, y0, yp0);
It has to be noted that the initial conditions t, y, yp should fulfill the equation f(t, y, yp)=0. Here an fsolve is used to satisfy this condition.
The use of annonymus functions is to define the function with the parameters as inputs and then execute the ODE solver having defined the parameters in the main code.

MATLAB offset when plotting integration of sin

I have a question, because this work for many functions, but I have a trouble when trying to plot the integral of a sine (I am using matlab 2010):
clear all
close all
clc
x = linspace(-10, 10, 100);
f = #(x) sin(x);
I = arrayfun(#(x) quad(f, 0, x), x);
plot(x, f(x),'r', x, I, 'b')
I expect having a -cos(x), but instead I get something with an offset of 1, why is this happening? How should fix this problem?
The Fundamental Theorem of Calculus says that the indefinite integral of a nice function f(x) is equal to the function's antiderivative F(x), which is unique up-to an additive constant. Further, a definite integral has the form:
In this form, the constant of integration will cancel out, and the integral will exactly equal the desired antiderivative only if the lower bound evaluation vanishes. However, -cos(0) does not vanish and has a value of -1. So in order to calculate the desired antiderivative F(x), the lower bound evaluation should be added to the right-hand side.
plot(x, f(x),'r', x, I+ (-cos(0)), 'b');
This is the equivalent of assigning an initial value for the solution of ODEs a la ode45.
What you're trying to do can be achieved using the following:
x = linspace(-10, 10, 100);
syms y;
f = sin(y) %function
I =int(f,y) %integration of f
plot(x, subs(f,y,x),'r', x, subs(I,y,x), 'b')
Output:-
According to Matlab documentation, q = quad(fun,a,b)
Quadrature is a numerical method used to find the area under the graph of a function, that is, to compute a definite integral.
Integral of sin(x) equals -cos(x)
Definite integral of sin(x) from x = pi to x = 0:
-cos(pi) - (-cos(0)) = 2
Since quad compute a definite integral, I can't see any problem.
Same as:
figure;plot(-cos(x) - (-cos(0)))

Interpolation within ode45

I have a vector with discrete values that I need to pass into my ODE system and I want to use ode45 command. This vector needs to be interpolated within the solver when I use it. Is there a way this can be done?
I have a coupled system of linear ODEs.
dxdt = f(t)*x + g(t)*y
dydt = g(t)*x + h(t)*y
I have three vectors f(t), g(t) and h(t) as a function of t. I need to pass them into the the solver.
I can code up a Runge-Kutta solver in C or C++. I was suggested that doing it in Matlab would be quicker. Can someone suggest a way to do this?
Sure, you can use interp1. Suppose you have vectors fvec, gvec and tvec containing respectively the values of f, g, and the time points at which these values are taken, you can define your derivative function as:
dxydt = #(t,x) [interp1(tvec, fvec, t) * x(1) + interp1(tvec, gvec, t) * x(2)
interp1(tvec, gvec, t) * x(1) + interp1(tvec, hvec, t) * x(2)];
and use it in ode45:
[T,Y] = ode45(dxydt, tspan, [x0; y0]);

Get coefficients of symbolic polynomial in Matlab

I have a Matlab function that returns a polynomial of the form:
poly = ax^2 + bx*y + cy^2
where a, b, and c are constants, and x and y are symbolic (class sym).
I want to get the coefficients of the polynomial in the form [a b c], but I'm running into the following problem. If the function returns poly = y^2, then coeffs(poly) = 1. I don't want this – I want it to return [0 0 1].
How can I create a function that will give me the coefficients of a symbolic polynomial in the form that I want?
You can use sym2poly if your polynomial is a function of a single variable like your example y^2:
syms y
p = 2*y^2+3*y+4;
c = sym2poly(p)
which returns
c =
2 3 4
Use fliplr(c) if you really want the coefficients in the other order. If you're going to be working with polynomials it would probably also be a good idea not to create a variable called poly, which is the name of a function you might want to use.
If you actually need to handle polynomials in multiple variables, you can use MuPAD functions from within Matlab. Here is how you can use MuPAD's coeff to get the coefficients in terms of the order of variable they precede (x or y):
syms x y
p = 2*x^2+3*x*y+4*y;
v = symvar(p);
c = eval(feval(symengine,'coeff',p,v))
If you want to extract all of the information from the polynomial, the poly2list function is quite helpful:
syms x y
p = 2*x^2+3*x*y+4*y;
v = symvar(p);
m = eval(feval(symengine,'poly2list',p,v));
c = m(:,1); % Coefficients
degs = m(:,2:end); % Degree of each variable in each term
The polynomial can then be reconstructed via:
sum(c.*prod(repmat(v,[size(m,1) 1]).^degs,2))
By the way, good choice on where you go to school. :-)
There is also an alternative to this problem. For a given degree, this function returns a polynomial of that degree and its coefficients altogether.
function [polynomial, coefficeint] = makePoly(degree)
syms x y
previous = 0 ;
for i=0:degree
current = expand((x+y)^i);
previous= current + previous ;
end
[~,poly] = coeffs(previous);
for j= 1:length(poly)
coefficeint(j) = sym(strcat('a', int2str(j)) );
end
polynomial = fliplr(coefficeint)* poly.' ;
end
Hope this helps.