I am trying to solve a system of two stochastic differential equations using "sde_euler". Here is my code:
function y = stoch_Fx_model(t0,tf,F0,x0)
r=1;
sig=1;
lambda=1;
h=1;
S=1;
f= #(t,Y)[r.*Y(1).*(1-Y(1)) -h.*Y(1).*(1-Y(2))./(Y(1)+S);
lambda.*Y(2).*(1-Y(2)).*(1+sig.*(2.*Y(2)-2.*Y(1)-1))];
g=#(t,Y)[0.5;0.6];
dt=0.001;
t=t0:dt:tf;
Y0=[F0;x0];
opts = sdeset('ConstGFUN','yes','NonNegative','yes');
y = sde_euler(f,g,t,Y0,opts);
y = min(y,1);
plot(t,y(:,1))
end
After specifying the values of t0,tf,x0,F0 I run the code and I am encountering the following error:
Error using sdearguments (line 22)
Input vector TSPAN must have length >= 2. See SDE_EULER.
Error in sde_euler (line 130)
[N,D,tspan,tdir,lt,y0,fout,gout,dgout,dg,h,ConstStep,dataType,NonNegative,...
Error in stoch_Fx_model (line 19)
y = sde_euler(f,g,t,Y0,opts);
I don't understand my mistake. my TSPAN is greater than 2 in length. Here is the source for the toolbox:
https://github.com/horchler/SDETools/blob/master/SDETools/sde_euler.m
Any help please?
thank you!
Related
without using conv() command, I want to convolve two signals:
This is the code I write:
syms n k i
f= #(n) 2.*(0<=n<=9);
h(n)=((8/9)^n).*heaviside(n-3);
L = length(f);
M = length(h(n));
out=L+M-1;
y=zeros(1,out);
for i = 1:L
for k = 1:M
y(i+k-1) = y(i+k-1) + h(k)*f;
end
end
However, I get an error:
Unable to perform assignment because value of type 'sym' is not convertible to 'double'.
Error in untitled7 (line 13)
y(i+k-1) = y(i+k-1) + h(k)*f;
Caused by:
Error using symengine
Unable to prove '(0.0 <= 0.0) <= 0.0' literally. Use 'isAlways' to test the statement mathematically.
I cannot find a way to fix it and also I do not know how to plot it at the end because of this. Can you help me in both of these issues please?
example how to convolve without command conv
nstop=20;
n1=[-nstop:1:nstop];
n0=nstop+1 % n1(n0)=0
h=zeros(1,numel(n1));
x1=zeros(1,numel(n1));
h(n0+[3:nstop])=(8/9).^[3:nstop];
x1(n0+[0:9])=2;
X1=flip(hankel(x1),2)
H=repmat(h,numel(n1),1);
conv_x_h=sum(X1.*H,2)';
n2=[0:numel(n1)-1]; % time reference for resulting conv
figure;
stem(n1,x1)
hold on
stem(n1,h);
grid on
legend('x1(n)','h(n)')
figure
stem(n2,conv_x_h)
grid on
title('conv(x1,h)')
I'm working in MATLAB and I have a problem with a system of differential equations. My system is dvdt = (-2*T)-4*v and dTdt = 6*T+v and I want to resolve it using Runge-Kutta of 4th order. I wrote the following code:
If I run only the function odefun, my code works! But my code didn't work if I run the complete program:
clear all
yy=#(t,y)[dvdt;dTdt]
[t,y]= ode45(#(t,y)yy,[0 1.5],[1 1/2 ]);
function res = odefun(t , y )
% i define two variable i will use: v and TT
v = y(1,:);
T = y(2,:);
% i define the two partial derivative:
res(1,:) = dvdt
res(2,:) = dTdt
dvdt = (-2*T)-4*v
dTdt = 6*T+v;
end
This is my results. I don't know to write a vector of a correct length:
Error using odearguments (line 95)
#(T,Y)YY returns a vector of length 1, but the length of initial conditions vector is 2. The vector returned by #(T,Y)YY and the initial conditions vector must have
the same number of elements.
Error in ode45 (line 115)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Error in sys_ode (line 4)
[t,y]= ode45(#(t,y)yy,[0 1.5],[1 1/2 ]);
y as passed from ode45 to odefun is a flat, one-dimensional array. Your variable assignment like you were dealing with a 2-dimensional array makes no sense. It should be v=y(1); T=y(2);.
In a code block, you need to observe causality. You need to declare/define a variable before you can use it in another code line.
Without calling odefun you will not get an error as each line in odefun is syntactically correct. However you get an error in its execution as the semantic is wrong.
Try
clear all
[t,y]= ode45(odefun,[0 1.5],[1 1/2 ]);
function res = odefun(t , y )
% i define two variable i will use: v and T
v = y(1);
T = y(2);
% i define the two derivatives:
dvdt = (-2*T)-4*v
dTdt = 6*T+v;
res = [ dvdt dTdt ]
end
I want to fit a curve to some data. I used a PCHIP interpolation because of getting the best results. Moreover, I want to get the coefficients for the 6 intervals with the ppval-function. But there pops up an error like these:
Error using unmkpp (line 18)
The input array does not seem to describe a pp function.
Error in ppval (line 62)
[b,c,l,k,dd]=unmkpp(pp);
Error in SA (line 8)
v = ppval(p,xdata)
This is my code:
clear all
xdata = [0; 3.5; 6.8; 7.6; 8.2; 30; 34.2];
ydata = [0; 50; 400000; 2000000; 25000000; 100000000;100000000]
xq1 = 0:0.01:35;
p = pchip(xdata,ydata, xq1);
s = spline(xdata,ydata,xq1);
v = ppval(p,xdata)
plot(xdata,ydata,'o',xq1,p,'-',xq1,s,'-.');
legend('Datenpunkte','pchip','spline','Location','SouthEast');
Can you help me?
Best regards
Dominik
pchip has two working modes:
Calculating the piecewise polynomial coefficients: pp = pchip(x,y):
returns a piecewise polynomial structure for use with ppval
Interpolating at specified points: p = pchip(x,y,xq):
is the same as p = ppval(pchip(x,y),xq)
returns a vector of interpolated values p corresponding to the query
points in xq
So, you are using the second mode, which is not suited for use with ppval.
I need to solve the spherical pde diffusion equation:
dq/dt=(1/r^2)*d/dr(r^2*D*(dq/dr))
D is a constant
initial condition is q=0 at t=0
boundary conditions are: dq/dr=0 at r=0, q=1 at r=Rc
I have written the following code but I am getting an error:
function pde1
m=2;
x=linspace(0.01,1,20);
t=linspace(0.01,2,10);
sol=pdepe(m,#pdefun,#pdeic,#pdebc,x,t);
u=sol(:,:,1);
% A surface plot
surf(x,t,u)
title('Surface plot')
xlabel('Distance x')
ylabel('Time t')
end
%-----------------------------------
function [c,f,s]=pdefun(x,t,u,DuDx)
global d
c=1/d;
f=DuDx;
s=0;
end
%-----------------------------------------------------
function [pl,ql,pr,qr]= pdebc(xl,ul,xr,ur,t)
global r
pl=0;
ql=0;
pr=1;
qr=r;
end
%------------------------------------------------
function u0 = pdeic(x)
u0=0;
end
could you please tell me what's wrong with it?
The error is:
Error using /
Matrix dimensions must agree.
Error in pdefun (line 3)
c=1/d;
Error in pdepe (line 246)
[c,f,s] = feval(pde,xi(1),t(1),U,Ux,varargin{:});
Error in pde1 (line 5)
sol=pdepe(m,#pdefun,#pdeic,#pdebc,x,t);
Thanks
I try to get an integral of two function handles in Matlab. The first function handle would be a weibull probability density function and the second function handle is based on a cfit I created with linear interpolation of single points.
x = 0:0.1:35;
fun1 = #(x) wblpdf(x,weibullAlpha,weibullBeta);
fun2 = #(x) feval(cfitObject,x);
fun3 = #(x) (fun(x).*fun2(x));
y = integral(fun3,0,35); % Using quad(fun3,0,35) doesn't work either.
I receive the following error:
Error using integralCalc/finalInputChecks (line 515)
Output of the function must be the same size as the input. If FUN is an array-valued integrand,
set the 'ArrayValued' option to true.
Error in integralCalc/iterateScalarValued (line 315)
finalInputChecks(x,fx);
Error in integralCalc/vadapt (line 132)
[q,errbnd] = iterateScalarValued(u,tinterval,pathlen);
Error in integralCalc (line 75)
[q,errbnd] = vadapt(#AtoBInvTransform,interval);
Error in integral (line 88)
Q = integralCalc(fun,a,b,opstruct);
Error in test (line 7) % "test" is the name of the script file.
y = integral(fun3,0,35);
The problem must have to do something with "fun2" since the code works just fine with e.g.
fun2 = x.^2;
Note: if I plot fun2 with the cfitObject I don't get an error. It's also possible to integrate the function using quad().
x = 0:0.1:35;
fun2 = #(x) feval(cfitObject,x);
y = quad(fun2,0,35);
plot(x, fun2(x))
Any help is greatly appreciated!
Your code seems to be ok. Probably the problem is that fun2 cannot take vectorized input, it could be resolved by modifying fun2 (cfitObject) to be able to handle vector input or telling the software that the function in the integral is array valued:
y = integral(fun3, 0, 35, 'ArrayValued', 1);