I want to find the equilibrium points of the following differential equation in Maple:
y' = h(t-1) with h(t) = 1 if t>= 0 and h(t) = 0 otherwise
I tried using piecewise equations like this:
piecewise(t >= 0, h(t) = 1);
but I don't know how to continue because I need to solve for h(t-1) = 0 and not for 'h(t) = 0`.
Not sure I follow your question entirely but will try. First, define h as an operator
h := t->piecewise(t>0,1,0);
Then just solve for the zeros of the derivative?
solve(h(x-1)=0,x);
Which gives you the result RealRange(-infinity,1) Hope this is what you want.
Related
Suppose i have an equation that looks like this: x = y + zx
I have the values for y and z, I want to calculate x. How would I do this on Matlab without having to solve for x on paper?
Probably a simple equation but I am new, thank you!
I tried just writing the equation out and coding disp(x), but of course it did not work.
The function fzero() will give you a numeric answer if you move all of one side of the equation over to the other side, so the first side is equal to zero.
y = 4;
z = 2;
% Move everything to the right side
zero_left_side = #(x)(y+z*x) - (x);
initial_guess = 1;
x_solution = fzero(zero_left_side, initial_guess);
I have the following code
x = linspace(-pi, pi, 1e3);
y = sqrt((x).^(1/2));
plot(x, real(y));
plot(x, imag(y));
The value at x=-1 on the real and imaginary plots are both 0.7071 (sqrt(0.5). Why is it not 0 (real) and 1 (imaginary)? When I enter this code:
real((-1)^(1/2))
imag((-1)^(1/2))
this gives me 0 (real) and 1 (imaginary) as expected.
Any help would be much appreciated.
Thanks,
Note that sqrt((x).^(1/2)) = x.^(1/4)
This is not all imaginary since (-i)^4 = i^4 = -1*-1 = 1. Consider what happens on the complex plane and you should be able to arrive at the conclusion that if y^4 = -1 then y = exp(i*(2*N-1)*pi/4) where N is any integer.
This leads to 4 unique solutions for y which are +/-sqrt(2)/2 +/- i*sqrt(2)/2. MATLAB returns the one where both real and imaginary are positive.
How can I solve trigonometric equations without loosing all the solutions in matlab ? For example :
solve(sin(theta) == 0, theta)
will return 0 but I want to know all the solutions, not only the first one.
You can add some conditions to your equation.
For example start by declaring a symbolic variable theta:
syms theta
And now add as many conditions as you need:
solve(sin(theta) == 0,theta>=-2*pi,theta<=2*pi, theta)
You can also set assumption on symbolic variable, it is more clear in my opinion.
assume(-2*pi <= theta <= 2*pi)
out = solve(sin(theta) == 0, theta)
In both case the output will be:
out =
0
pi
-pi
-2*pi
2*pi
If needed, you can order the result with:
sym(sort(double(out)))
I have this non-linear process that sort of looks like GBM, but is not because of the square-root noise. Both Mu's are constants, and l (in front of one Mu, and sigma) is a parameter. Sigma is a constant too. N is a population that increases.
This is not easily solved analytically.
Ultimately, I'm interested in starting off a bunch of these guys in Matlab with "continous" time steps, vary the parameter l for each process, and see what that looks like.
Since I've never done anything with SDE's in Matlab, I am a little lost. I've had a look at the different SDE solvers, but I can't seem to make them work. As said, I'm not hoping to solve anything, just manipulate different population sizes, time, and this parameter l.
Anyone who can point me in the right direction?
Based on Desmond J. Higham I ended up with this, sort of ugly looking approach. It's quite slow. If anyone had any suggestions as to how I would vectorize or include a SDE solver, such as SDETools to simulate it faster, I would be very grateful.
clear;
clc;
clf
T = 35;
N = 2^12;
Delta = T/N;
lambda = 0.1;
sigma = 4;
Xzero = 1;
P = 500;
Xem = zeros(1,N+1);
Xem(1) = Xzero;
for i = 1:P
for j = 1:N
if log(Xem(j)) < 0
Xem(j) = nan;
end
Winc = sqrt(Delta)*randn;
Xem(j+1) = Xem(j) + lambda*Delta*Xem(j) + sigma*sqrt(Xem(j))*Winc;
end
plot(0:Delta:T,log(Xem))
xlabel('t','FontSize',16), ylabel('X','FontSize',16)
hold on;
end
In my experiment, I need to approximate or fitting a measurement y = f_m(x) with n linear segments. Value of n can be selected to be 1, 2, 3, 4, 5... and probably less than 10. For clarity, it's good if the errors from different cases can be compared to find the one with smallest error.
I've found one example using MATLAB (link):
% Random data...
xdata = linspace(-2,3,101);
ydata = log(abs(10./(10+1i*10.^xdata))) + 0.5*randn(size(xdata));
plot(xdata,ydata)
F = #(B,xdata) min(B(1),B(2)+B(3)*xdata); %Form of the equation
IC = [max(ydata) max(ydata) 0]; %Initial guess
B = lsqcurvefit( F,IC,xdata,ydata,[min(ydata) -inf -inf],[max(xdata) inf 0]);
hold all;
plot(xdata,F(B,xdata));
a = (B(1) - B(2)) / B(3)
cte = B(1)
c = B(2)
d = B(3)
This is similar to what I'm looking for in the case of 2 segments. I tried to modify this function to suit my need with changing the function handle:
F = #(B,xdata) min(B(1),B(2)+B(3)*xdata); %Form of the equation
to
F = #(B,xdata) min(B(1)+B(2)*xdata,B(3)+B(4)*xdata);
but it seems my modification results in 2 segments on the same line.
I don't know much about MATLAB function handle. Especially here, there is "min" function in it. Moreover, how should I do to extend this example to several linear segments?
Thank you in advance!!
Edit01:
Thanks!! your answer has made my code run as desired. But, may I ask a little on the question here. As previously mentioned, I originally want to extend the approximation to several linear segments. So, I go like:
F = #(B,xdata) min(B(1)+B(2)*xdata, B(3)+B(4)*xdata, B(5)+B(6)*xdata); %Form of the equation
IC = [max(ydata) max(ydata) max(ydata) max(ydata) max(ydata) 0]; %Initial guess
B = lsqcurvefit( F,IC,xdata,ydata,[min(ydata) -inf -inf -inf -inf -inf],[max(xdata) inf inf inf inf 0]);
but MATLAB response with I.C. error:
Failure in initial user-supplied objective function evaluation
Can you help me shortly with the I.C here? and what's with the "min" function in function handle?
I get the following error when running your code :
??? Error using ==> lsqncommon at 101
LSQCURVEFIT cannot continue because user supplied objective function failed with the following error:
Attempted to access B(4); index out of bounds because numel(B)=3.
Hence, this means that there is nothing in B(4). I would try to modify IC, lb and ub to have 4 elements.
So try to put these two lines in your code :
IC = [max(ydata) max(ydata) max(ydata) 0]; %Initial guess
B = lsqcurvefit( F,IC,xdata,ydata,[min(ydata) -inf -inf -inf],[max(xdata) inf inf 0]);