Implementing Euler's method for solving differential equations - matlab

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.

Related

Unable to plot vector field using quiver in Matlab

I am quite new to Matlab so I apologize if I am making some obvious mistakes, but I have a second order ordinary differential equation that I am trying to plot the vector field of. I have looked at various resources on this site, the MATLAB documentation and MATLAB forums but I am still having trouble.
I have tried solving the ODE with the ode45 function, this gives my a single solution that I can plot. I am using the function odeToVectorField to transform my ODE into a system of first order ODE. I have a feeling this is what is preventing me from properly doing what I want because it give me a set of symbolic equations that I am difficulty to evaluate over the meshgrid.
Here is my code so far:
% Setup outside variables
clear
mu = 0.75; % resistance variable
l = 1; % length of pendulum
g = 9.81; % gravity
% Next we setup the domains of the vector space.
xdom = linspace(-pi,pi,51); % define the X axis space
ydom = linspace(-pi,pi,51); % define the Y axis space
[X,Y] = meshgrid(xdom,ydom); % create a meshgrid for the vector field
% Define the system of ODEs
syms y(t)
eqn = diff(y,t,2) == - mu.*diff(y,t,1) - (g/l).*sin(diff(y,t,0));
U = odeToVectorField(eqn) % this is the part where I think I am doing wrong
Uf = matlabFunction(U(1), 'vars',{'t','Y'}) % trying to set a function handle for the first ODE
Vf = matlabFunction(U(2), 'vars',{'t','Y'}) % trying to set a function handle for the second ODE
% Now we plot it
figure
quiver(X,Y,Uf(X,Y), Vf(X,Y))
Here is a screenshot of my output. I have purposefully left out some semi colons to help you visualize what I am trying to do.
Thank you very much for your help.

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.

Simple script that computes a solution of linear ODEs giving wrong result

this a question that envolves both programming and mathematics. So, I'm trying to write a code that computes the general solution of a system of linear ODEs described by . The mathematical formula it's shown above:
where the greek symbol \PHI that appers in the equation is the expm(A*t)
clear all
A=[-2]; %system matrix
t0=1; %initial time of simulation
tf=2; %final time of simulation
syms t x_0
x0=x_0;
hom=expm(A*t); %hom means "homogeneous solution"
hom_initialcond=hom*x0;%this is the homogeneous solution multiplied by the initial conditon
invhom=inv(hom); %this is the inverse of the greek letter at which, multiplied by the input of the system, composes the integrand of the integral
g=5*cos(2*t); %system input
integrand=invhom*g; %computation of the integrand
integral=int(integrand,t0,t); %computation of the definite integral from t0 to t, as shown by the math formula
partsol=hom*integral; %this is the particular solution
gen_sol=partsol+hom_initialcond %this is the general solution
x_0=1; %this is the initial condition
t=linspace(t0,tf); %vector of time from t0 to tf
y=double(subs(gen_sol)); %here I am evaluating my symbolic expression
plot(t,y)
The problem is that my plot of the ODE's solution it's not looking well, as you can see:
The solution it's wrong because the curve shown in the graph doesnt start at the initial value equals 1. But the shape it's very similar from the plot gave by the MATLAB ODE solver:
However, if I set t0=0 then the plot gave by my code and by MATLAB solver it's exacly equal to each other. So, my code it's fine for t0=0 but with any other values my code goes wrong.
The general solution in terms of fundamental matrices is
or more often seen as
But since the initial time is often taken to be zero, the inverse of the fundamental matrix is often omitted since it is the identity for linear, constant coefficient problems at zero (i.e., expm(zeros(n)) == eye(n)) and the c vector is equivalent to the initial condition vector.
Swapping some of the lines around near your symbolic declaration to this
syms t x_0 c_0
hom = expm(A*t) ;
invhom = inv(hom) ;
invhom_0 = subs(invhom,t,sym(t0)) ;
c_0 = invhom_0 * x_0 ;
hom_initialcond = hom * c_0 ;
should provide the correct solution for non-zero initial time.

How to do MCMC simulation using Metropolis hasting algorithm in Matlab?

I am trying to simulate a distribution for parameter theta f= theta ^(z_f+n+alpha-1)*(1-theta)^(n+1-z_f-k+ beta-1), where all the parameter except for theta is know. I am using Metro polish hasting algorithm to do the MCMC simulation . My proposal density is a beta distribution with parameter alpha and beta. My code for the simulation are as follows. I am using a buitlin Matlab code called mhsample() for this purpose, How do I know if my code is working properly?
clear
clc
alpha=2;
beta=2;
z_f=1;
n=6;
k=5;
nsamples = 3000;
pdf= #(x) x^(z_f+n+alpha-1)*(1-x)^(n+1-z_f-k+beta-1); % here x acts as theta
proppdf= #(x,y) betapdf(x, alpha, beta);
proprnd =#(x) betarnd(alpha,beta,1);
smpl = mhsample(0.1,nsamples,'pdf',pdf,'proprnd',proprnd,'proppdf',proppdf);
I'm unsure of what you're asking when you say "how do I know if my code is working properly" -- I'm assuming it executes? But for a visual comparison of your function vs. the simulation, you can plot both the PDF and the data you got from mhsample as follows:
% i'm assuming you ran the code above so that smpl and #pdf are both defined...
fplot(pdf,[0 1]); % fplot takes your function and plots it between x-limit [0,1]
figure % new figure
hist(smpl,30); % 30 here is bin size, change it to your preference
Figure below:
the histogram of smpl's output on left, i.e., your simulation
the function pdf bounded in [0,1] on right for comparison to your simulation
This was just a wild guess because those two figures resemble each other and are also beta-distribution-esque.
If you want a more complex analysis than that, I'm afraid I'm not yet proficient in MCMC :)

Quantile regression with linprog in Matlab

I am trying to implement the quantile regression process with a simple setup in Matlab. This page contains a description of the quantile regression as a linear program, and displays the appropriate matrices and vectors. I've tried to implement it in Matlab, but I do not get the correct last element of the bhat vector. It should be around 1 but I get a very low value (<1e-10). Using another algorithm I have, I get a value of 1.0675. Where did I go wrong? I'm guessing A, b or f are wrong.
I have tried playing with optimset, but I don't think that is the problem. I think I've made a conversion mistake when going from math to code, I just can't see where.
% set seed
rng(1);
% set parameters
n=30;
tau=0.5;
% create regressor and regressand
x=rand(n,1);
y=x+rand(n,1)/10;
% number of regressors (1)
m=size(x,2);
% vektors and matrices for linprog
f=[tau*ones(n,1);(1-tau)*ones(n,1);zeros(m,1)];
A=[eye(n),-eye(n),x;
-eye(n),eye(n),-x;
-eye(n),zeros(n),zeros(n,m);
zeros(n),-eye(n),zeros(n,m)];
b=[y;
y
zeros(n,1);
zeros(n,1)];
% get solution bhat=[u,v,beta] and exitflag (1=succes)
[bhat,~,exflag]=linprog(f',A,b);
I solved my problem by using the formulation (in the pdf) above the one I tried to implement in the question. I've put it in a Matlab-function if you're interested in the code.
function [ bhat ] = qregressMatlab( y, x, tau )
% bhat are the estimates
% y is a vector of outcomes
% x is a matrix with columns of explanatory variables
% tau is a scalar for choosing the conditional quantile to be estimated
n=size(x,1);
m=size(x,2);
% vectors and matrices for linprog
f=[tau*ones(n,1);(1-tau)*ones(n,1);zeros(m,1)];
Aeq=[eye(n),-eye(n),x];
beq=y;
lb=[zeros(n,1);zeros(n,1);-inf*ones(m,1)];
ub=inf*ones(m+2*n,1);
% Solve the linear programme
[bhat,~,~]=linprog(f,[],[],Aeq,beq,lb,ub);
% Pick out betas from (u,v,beta)-vector.
bhat=bhat(end-m+1:end);
end