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

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.

Related

How can I find the total harmonic distortion of a nonlinear signal. Using matlab

How can I find the total harmonic distortion of a nonlinear signal. For example, Forced Van der pol Oscillator with code as shown below. I have tried the 'thd' function in matlab but I guess I'm missing somethings.
This is the equation
x''-mu(1-x^2(t))x'(t)+x(t)=Pcos(w*t)
function vdpo()
t=0:0.001:10
mu=2
x0=-2;
v0=2;
p=10; w=7;
[t,x]= ode45(#f, t, [x0,v0])
function dxdt=f(t,x)
dxdt1=x(2); dxdt2= mu(1-x(1)^2)*x(1)+p*cos(w*t);
dxdt=[dxdt1 ;dxdt2];
end
end
Try the code below, in which function f(t,x) is our ODE equations and we call function ode45 to use Runge-Kutta methods to solve it.
function [x]=vdpo()
t=0:0.001:10
mu=2
x0=-2;
v0=2;
p=10; w=7;
[t,x]= ode45(#f, t, [x0,v0])
function dxdt=f(t,x)
dxdt1=-x(2)-x(1)+(x(1)^3)/3; dxdt2=-x(1)+p*cos(w*t);
dxdt=[dxdt1 ;dxdt2];
end
end
However, it is actually a math problem rather than a programming problem. The first thing that we have to do is to transform the equations into a more convenient form by defining y=x'+((x^3)/3-x)*mu, then we have 2 First Order Ordinary Differential Equations so we could call ode45 to solve it. I looked them through at here(page2).
By calling
X=vdpo();
x=X(:,1);
thd(x)
we could get an answer like:
p.s. NOT CERTAIN about THD part.

Implementing Euler's method for solving differential equations

This code is to find the Euler's method on MATLAB
function [x,y]=euler_forward(f,xinit,yinit,xfinal,n)
h=(xfinal-xinit)/n;
% Initialization of x and y as column vectors
x=[xinit zeros(1,n)]; y=[yinit zeros(1,n)];
% Calculation of x and y
for i=1:n
x(i+1)=x(i)+h;
y(i+1)=y(i)+h*f(x(i),y(i));
end
end`
'f=#(x,y) (1+2*x)*sqrt(y);
% Calculate exact solution
g=#(x,y) (1+2*x)*sqrt(y);
xe=[0:0.01:1];
ye=g(xe);
[x1,y1]=euler_forward(f,0,1,1,4);
% Plot
plot(xe,ye,'k-',x1,y1,'k-.')
xlabel('x')
ylabel('y')
legend('Analytical','Forward')
% Estimate errors
error1=['Forward error: ' num2str(-100*(ye(end)-y1(end))/ye(end)) '%'];
error={error1}
So I have this so far for the problem and the it gives an error saying y is not defined. What do I do?
The problem is here:
% Calculate exact solution
g=#(x,y) (1+2*x)*sqrt(y);
Exact solution is a function of one variable, x. Presumably, you were supposed to find it with paper and pencil (or with Wolfram Alpha, etc): it is
g=#(x) .25*(x.^2+x+2).^2
Then the code works as expected, producing this neat chart: as is typical for Euler's method, the numerical solution lags behind the exact one.

matlab: using a vector of unknowns in fsolve

I need to solve a system of nonlinear equations; in order to use fsolve , I have written an m-file containing my function "myfun". This function is called by the main m-file.
Both the system and the unknowns must be written using a "for" loop.
Example:
function F=myfun(x)
n=20;`
for j=1:n
c1=sqrt(x(j)^2-3*x(j));
c2=x(j)^(1/2);
F(j)=c1+c2;
end
My problem is that I have to preallocate memory for my vectors, both F and x, otherwise the solver considers numel(x)=1.
But if I declare
F=zeros(n,1);
x=zeros(n,1);
I have the following output:
No solution found.
fsolve stopped because the problem appears regular as measured by the gradient,
but the vector of function values is not near zero as measured by the
default value of the function tolerance.
Any suggestions? Thanks
You don't need the loop, just use
F = sqrt(x.^2-3*x) + x.^(1/2);
Then you also don't need to declare n, c1, c2.
Your error message also doens't sound like it's a problem with the allocation, but more with finding a solution to you fsolve problem.

Solving systems of nonlinear equations

Help. I am trying to solve this system of nonlinear equations in MATLAB for a homework assignment. I have tried wolfram alpha and this online equation solver, and neither of them work.
I have tried my graphing calculator and it keeps saying non algebraic variable or expression.
These are my two equations in two unknowns:
.75*(1100)= x*10^(6.82485-943.453/(T+239.711))
25*1100=(1-x)*10^(6.88555-1175.817/(T+224.887)
I don't quite understand how to use MATLAB to solve this system. Please help.
You want the function fsolve in Matlab. Define a function myfun that returns [0,0] at the solution, then run fsolve(myfun,x0). x0 is a guess for the solution.
Define myfun:
function F = myfun(x)
F = [<put modified eqt1 here>;
<put modified eqt2 here>;];
Save it. Then solve:
x0 = [1,1];
options = optimoptions('fsolve','Display','iter');
[x,fval] = fsolve(#myfun,x0,options) % Call solver

Partial derivative with Matlab Symbolic Toolbox for Lagrangian equations of motion

I'm trying to derive Lagrangian equations of motion in Matlab using the symbolic toolbox. This involves partial derivatives of a function and your coordinates, but matlab seems to not accept this.
So I would do this in Matlab:
syms t x(t) % t: time, x(t) position dependent on time
m = sym('m'); % mass, a constant parameter
T = m/2*diff(x,t)^2; % kinetic energy
dTdx = diff(T,x);
ddTdxDotdt = diff( diff(T,diff(x,t)), t);
But as soon as I try to differentiate anything in x (or diff(x,t)), Matlab complains:
Error using mupadmex
Error in MuPAD command: The variable is invalid. [stdlib::diff]
Error in sym/diff (line 44)
R = mupadmex('symobj::diff', S.s, x.s, int2str(n));
Does anyone know the proper way of handling this?
Matlab ought to be able to do this as you have it written, but I think that it doesn't like taking derivatives with respect to a symfun. Type whos in the command window and you'll see that x is listed as a symfun while t is just a sym. The help for diff kind of indicates this limitation. It won't event try to take the derivative of a constant with respect to x(t): diff(1,x) "complains" just the same. Unless newer versions of Matlab fix this (I'm on R2012b) I think you only option may be to come up with a scheme using two instances of x.