Trouble plotting a solution curve with dsolve - matlab

I'm trying to do my assignments which is plotting a direction fields and a solution curve that passes a given point and having a trouble doing this particular differential equation: y'=1-x*y, y(0)=0.
My code is :
syms x y;
y1=dsolve('Dy=1-x*y','y(0)=0','x');
y1=expand(y1);
ezplot(y1,[-10 10 -10 10]);
And it got some error about the input i believe, it says:
Error using inlineeval and error in expression, input must be real and
full and so on...
I have had success with other differential equations but this one is still miserable.

I'm almost sure this is a comment, but since I still don't have enough reputation to do so I'm answering here :P
Apparently the solution y1 for your equation is something like
y1 = -(2^(1/2)*pi^(1/2)*exp(-x^2/2)*erf((2^(1/2)*x*1i)/2)*1i)/2
The erf function needs a real input, but in this case you have a 1i complex term which is causing the problem.

Related

Can't solve ODE in matlab, strange plot occur

enter image description here
I've checked my code, which I think is ok, but can't get the right plot as in the book
here's my code:
clc,clear,close all
syms y(x) dy d2y
dy=diff(y,1)
d2y=diff(y,2)
y=dsolve((1-x)*diff(y,2)==sqrt(1+diff(y,1)^2)/5,y(0)==0,dy(0)==0)
fplot(y,[0,2],'r')
the output of y = dsolve is a vector with 2 equations as its elements. Also in the range of fplot these functions are complex. Since I don't know which results you are looking for, you can try and check these alternatives for your plot and have a look at the output:
fplot(real(y(1)),[0,2],'r')
fplot(abs(y(1)),[0,2],'r')
fplot(imag(y(1)),[0,2],'r')

Solve system of differential equation with embedded non diferential equations, using Octave/Matlab (see picture)

I have the following equation system (click to see picture)
, and would like to solve for X(t), Y(t), Z(t), hopefully using Octave/Matlab, which I'm familiar with, but I would not mind solving it by any other means necessary.
Now, Fsolve is useful for regular equation systems, and Ode45, Lsode are useful for differential equations. But, what about this particular system? Note that the differential equation at the bottom contains not only Y, but, also, both X and Z, and those are dependent on the two non-differential equations above.
To be honest, I'm not quite sure how to approach a basic code to solve this system, and after spending some time thinking, I decided to ask for help. I really appreciate any guidance to solve this problem somewhat efficiently, if it is even possible. Almost any reply would be of some use to me right now.
If you know y, you can solve for x, and this even unconditionally as the second equation in monotonous in x
x = fsolve(#(x) y^2-1413.7*x-1095.2*cos(x)+2169, 0)
Then once you know x, you can solve for z using the known inverse cosine
z = acos(0.20978-cos(x))
This may actually fail to give a result if cos(x) comes close to -1. One can artificially cut out that error, introducing a possibly wrong solution
z = acos(min(1,0.20978-cos(x)))
For simplicity, assemble these operations in a helper function
function [x,z] = solve_xz(y)
x = fsolve(#(x) y^2-1413.7*x-1095.2*cos(x)+2169, 0);
z = acos(min(1,0.20978-cos(x)));
end
Now use that to get the ODE for y
function dy = ode_y(t,y)
[x,z] = solve_xz(y(1));
dy = [ y(2); y(3); 6666.6667*(z-x)-333.3333*y(1)-33.3333*y(2)-5*y(3) ];
end
and apply the ODE solver of your choice. It is quite likely that the system is stiff, so ode45 might not be the best solver.

compute derivative of first order function

I have a problem when computing the derivative of a first order function as below:
syms x(t)
xd = diff(x);
y = xd*xd;
how to compute derivative of y by xd;
functionalDerivative(y,xd);
Then, it raises an error as below:
Error using symengine
The variable 'diff(x(t), t)' is invalid.
The result should be:
2*diff(x,t)
I also think about name xd as a symbolic variable, then use diff(y,xd) but this way is not good for some situation. Do we have any method can directly compute the derivative of a differential function?
Please suggest me some solutions.
Thank in advance!
I find it a little odd that the Symbolic Engine can't handle that, lacking a better word I'll borrow one from the TeX world, unexpandable expression. However, you can get around the limitation by subs-ing in a temporary variable, taking the derivative, and subs-ing it back in:
>> syms u(t)
>> dydxd = subs(functionalDerivative(subs(y,xd(t),u(t)),u),u(t),xd(t))
dydxd(t) =
2*diff(x(t), t)
Hopefully this subs approach will work in most cases, but more complex expressions for y may make it not work.

error solving two coupled second order differential equations in matlab

I am trying to solve the following differential equations on matlab. (They are the equations obtained from the yang-mills-higgs lagrangian for the hoofy polyakov monopole ansatz). This is my function file. I have two variables h and k and their derivatives w.r.t to a variable t. My x(1)=h, x(2)=k, x(3)=dh\dt, x(4)=dk\dt. All the functions have initial value 0.
function xprime = monopole( t,x )
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
xprime(1)=x(3);
xprime(2)=x(4);
xprime(4)=(1/(t.^2)).*((x(2).^2)-1).*x(2) + 4.*(x(1).^2).*x(2);
xprime(3)=(2/(t.^2)).*(x(2).^2).*x(1)-(1-(x(1)).^2).*x(1)-(2/t).*x(3);
xprime=xprime(:);
end
Now when I run the following code
>
> t0=0;
>> tf=10;
>> x0=[0 0 0 0];
>> [t,s]=ode45(#monopole,[t0,tf],x0);
>> plot(t,s(:,1));
I am not getting anything. The graph window appears but it doesnt contain anything. This equations are supposed to have solutions. The dotted curves is what one should get with the curve starting from 1 is k, and from 0 is h.
What is my mistake?
When this happens the first thing you should do is look at the values in the t and s vectors. In this case s(1,1) contains 0, and s(:,2:end) are all NaN. Hence nothing on the plot.
As to why this is happening, a few thoughts
Are you sure that your definition of monopole is correct?
Why are you showing a plot where k(0) = 1, but passing it an initial condition of k=0?
Why are you using h^prime(0) = 0 initial conditions, but in the plot it looks like h^prime(0) has a non-zero slope?
The term with the 1./t^2 sure looks suspicious; just think about it, at the very first step you are going to divide 0 by 0, hence NaN. Perhaps the ode solver is having a hard time with this, and another solver would work better (note: I have very little experience with ODE solvers, so take this with a big grain of salt).
Finally, to make sure you really understand how to use the ODE solver, why not start with a very simple ODE where you know the exact answer (i.e. harmonic oscillator).

How can I solve a Volterra (Fredholm?) integral equation in Matlab

I'm talking about Volterra integral equations of second order:
In my case g is an ugly integral also between a and x, also a=0 (for both g and the integral above). K is equal to 1.
I found some information about Fredholm equations, but they are not exactly the same (fixed intervals, they don't have x on the integral sign), I wonder if maybe I can reconduct my analysis to a Fredholm equation? And if so, how can I solve it in Matlab?
You have a Volterra equation of the second kind, and in the case when the kernel takes the form k(x-t) it can be solved an operational way(method).
Since your task is not unique, will be convenient to use a ready solution:
http://www.mathworks.com/matlabcentral/fileexchange/49721-volterra-integral-equations-solver
Solve your equation (if I understood correctly problem statement):
isolve(1,10*x,1)
ans =
10*exp(x) - 10
Will check the correctness of the solution by substituting in the source:
10*x + int(y,0,x)
ans =
10*exp(x) - 10
Solved correctly.
P.S. Sorry for my English.