Octave: How to solve a one order differential equation with variable coefficients - octave

I'm trying to solve a one order differential equation with variable coefficients of the following form:
xdot(1)=a(t)*x(1)+b;
where b=a constant and where a(t) = a time dependent function. I know that I can solve this equation by hand butt a(t) is a quite complex function.
So, my problem is the following. a(t) is a function which I know its values from an experiment (I've got all the results in a file) --> a(t) is a vector (n x 1) which is a problem because x(1)= xdot(1)=a scalar. So, how could I solve this equation, with lsode ?

Possibly I have underestimated your problem, but the way I read it, you are asking to integrate a first order ODE. In general there are two ways to proceed,, implicit methods and explicit methods. Here is the crudest, but easiest to understand, method I can come up with:
nt=101;a=-ones(1,nt);b=1/2;x=NaN*ones(1,nt);x(1)=pi;dt=0.01;
for kt=2:nt
dxdt=a(kt-1)*x(kt-1)+b;
x(kt)=x(kt-1)+dxdt*dt;
endfor
plot(x)
I have assumed a<0 so there is no tendency to blow up. You will want to set it equal to your observed values.

Related

understanding the link between octave code and assignment equations

I have been struggling with some questions from my study guide and really am stuck - I have asked the lecturer for help but his answer was literally "but it's been done for you" (referring to gauss_seidel code that was written) - to which I think he missed the point. I'm struggling to understand the actual question and how to approach it.
The first question reads as follows:
Define the 100x100 square matrix A and the column vector b by:
A(ij)=I(ij)+1/((i-j)2+1) b_(i)=1+2/i 1<=i j<=100
where I_(ij) is the 100x100 identity matrix (i.e 1 on the main diagonal and 0 everywhere else). Solve for x using both the Gauss-Seidel method and the A\b construct.
We have written the code for the gauss_seidel method, and i think i understand what it does mostly, however, i do not understand how the above question fits into the method. I was thinking that i'm supposed to do something like the following in the octave window then calling the gauss_seidel method:
>> A=eye(100,100);
>> b= (this is where i get slightly confused)... I've tried doing
>> for b=1:n;
>> b=1+(2/n);
That is question 1.
Question 2 I have given an answer and asked him about but he has not responded.
It reads: The Hilbert matrix is a square n x n matrix defined by:
H_(ij)n = 1/i+j+1
Define bn to be a column vector of dimension n, and with each element 1. Construct bn and then solve for x, Hn xn=bn in the cases n=4.
What i did here was simply:
>> b=ones (4,1);
>> x=hilb(4)\b;
and then it gave me the output of x values. Im not sure if what i did here was correct... since it doesnt mention using any method at all it just says solve for x.
Im not sure how to relate the lecturers reply to understanding the problem.
If you could help me by maybe letting me know what im missing or how i should be thinking about this, it would really help.
the gauss_seidel code looks like this:
function xnew=gauss_seidel(A,b,xold)
n=size(A)(1);
At=A;
xnew=xold;
for k=1:n
At(k,k)=0;
end
for k=1:n
xnew(k)=(b(k)-At(k,:)*xnew)/A(k,k);
end
endfunction
Ive been writing pseudo since last Monday and I am only a little bit clearer on what the code does.
A(ij)=I(ij)+1/((i-j)2+1), b(i)=1+2/i, 1<=i, j<=100
All this is really saying is that we have to create A and b in such a way that i>=1 and j<=100. After doing that, you simply solve using the Gauss Seidel method.
So we'd create b like this:
b=zeros(100,1);
for k=1:100
b(k) = 1+(2/k);
end
This will create a column vector with a size of 100x1 with all the values that satisfy b(i)=1+2/i where i (or in the code,'k') was greater or equal to 1.
Then to create A :
myMatrix=zeros(100,100);
for i=1:100
for j=1:100
myMatrix(i,j) = 1/(((i-j)^2) + 1);
end
end
A=eye(100) + myMatrix;
Now we have created A in such a way that it equals A(ij)=I(ij)+1/((i-j)2+1) where i was greater or equal to 1 & j was less than or equal to 100.
The rest of the question is basically asking to to solve for the values of x using the Gauss Seidel method.
So it be something like this :
y=iterative_linear_solve(A,b,x0,TOL,max_it,method);
Don't forget about creating x0 as the initial assumption, tolerance and max iterations etc.
In terms of question 2, you did exactly what I would have done. I think you're good with that.
I'm not too sure how to answer this :
If you could help me by maybe letting me know what im missing or how i
should be thinking about this, it would really help.
All I can really say is that you need to look at the problems in such a way that you see Ax=b. For example in the first question we started by making b, and then A. After that we simply applied the A\b construct or the Gauss Seidel method and got our answer.
And that's essentially what you did for the second question.
Lastly, are you a UNISA student by chance? I am, haha. I've been struggling with this on my own for a while. The study guides don't seem to give a lot of info.

How to Solve non-specific non-linear equations?

I am attempting to fit a circle to some data. This requires numerically solving a set of three non-linear simultaneous equations (see the Full Least Squares Method of this document).
To me it seems that the NEWTON function provided by IDL is fit for solving this problem. NEWTON requires the name of a function that will compute the values of the equation system for particular values of the independent variables:
FUNCTION newtfunction,X
RETURN, [Some function of X, Some other function of X]
END
While this works fine, it requires that all parameters of the equation system (in this case the set of data points) is hard coded in the newtfunction. This is fine if there is only one data set to solve for, however I have many thousands of data sets, and defining a new function for each by hand is not an option.
Is there a way around this? Is it possible to define functions programmatically in IDL, or even just pass in the data set in some other manner?
I am not an expert on this matter, but if I were to solve this problem I would do the following. Instead of solving a system of 3 non-linear equations to find the three unknowns (i.e. xc, yc and r), I would use an optimization routine to converge to a solution by starting with an initial guess. For this steepest descent, conjugate gradient, or any other multivariate optimization method can be used.
I just quickly derived the least square equation for your problem as (please check before use):
F = (sum_{i=1}^{N} (xc^2 - 2 xi xc + xi^2 + yc^2 - 2 yi yc + yi^2 - r^2)^2)
Calculating the gradient for this function is fairly easy, since it is just a summation, and therefore writing a steepest descent code would be trivial, to calculate xc, yc and r.
I hope it helps.
It's usual to use a COMMON block in these types of functions to pass in other parameters, cached values, etc. that are not part of the calling signature of the numeric routine.

How can I solve this kind of equation in Maple?

equation1:
solve({a^2+b^2+169+sqrt(c-13)-24*a-10*b = 0},{a, b, c})
assuming a>0, b>0, c>0;
//a=12, b=5, c=13
equation2:
solve([1/(cos(a)^2)+1/(sin(a)^2*sin(b)^2*cos(b)^2) = 9,
a>0, a<Pi/2, b>0, b<Pi/2], [a,b,c] );
//a=arctan(sqrt(2)), b=Pi/4
I have tired above, but maple couldn't gives a solutions, Am I using solve incorrectly?
In (Eq. 1) it's not your syntax that's an issue. You have three unknowns {a,b,c} but only one equation. You simply do not have enough equations to determine {a,b,c} uniquely. Maple's solve function only returns an answer (if possible) if the number of variables equals the number of equations.
In (Eq. 2) you use square brackets, which are used for ordered lists. The solve function requires a set of equations, which are indicated by curly braces. Again, you have three variables but only one equation. Same problem.
If the equations are linear (which they aren't in your case), Maple can find a parameterization for the solutions in the case of an underdetermined system: http://www.maplesoft.com/support/help/Maple/view.aspx?path=solve/linear.

matlab function which is a function of an intergral

I need to write my own function which has the form f(x,y)=Integrate(g(x,y,z),z from 0 to inf). so the code I used was:
function y=f(x,y)
g=#(z)exp(-z.^2)./(z.^x).*(z.^2+y.^2).^(x/2);% as a function of x,y and z
y=quadgk(g,0,inf)
and if I call it for a single value like f(x0,y0), it works but if I try to calculate something like f([1:10],y0), then the error message says that there is something wrong with the times and dimension. In principle I can use for loops but then my code slows down and takes forever. Is there any help I can get from you guys? or references?
I'm trying to avoid the for loop since in matlab it's much faster to use matrix computation than to use for loop. I wonder if there is any trick that I can take advantage of this feature.
Thanks for any help in advance,
Lynn
Perhaps you can try to transpose the interval, creating row based values instead of column based f([1:10]',y0). Otherwise something in your function might be wrong, for example to get x^y to work with lists as input, you have to prefix with a dot x.^y. The same for mulitply and division I think..
If loop is no problem for you, you should do something like:
function y2=f(x,y)
y2=zeros(size(x));
for n=1:numel(x)
g=#(z)exp(-z.^2)./(z.^x(n)).*(z.^2+y.^2).^(x(n)/2);% as a function of x,y and z
y2(n)=quadgk(g,0,inf)
end
The problem here is that quadk itself uses vectors as argument for g. Then you have in g somethink like z.^x, which is the power of two vectors that is only defined if z and x have the same dimension. But this is not what you want.
I assume that you want to evaluate the function for all arguments in x and that the output vector has the same dimension as x. But this does not seem to be possible since even this simple example
g=#(x)[x;x.^2]
quad(g,0,1)
does not work:
Error using quad (line 79)
The integrand function must return an output vector of the same length as the
input vector.
A similar error shows when using quadgk. The documentation also says that this routine works only for scalar functions and this is not surprising since an adaptive quadrature rule would in general use different points for each function to evaluate the integral.
You have to use quadvinstead, which can integrate vector valued functions. But this gives wrong results since your function is integrated in the interval [0,\infty).

Finding the Maximum

How to find the following Maximum or supremum by computer software such as Mathematica and Matlab: $\sup\frac{(1+s)^{4}+(s+t)^{4}+t^{4}}{1+s^{4}+t^{4}}$?
Instead of numerical approximation, what is the accurate maximum?
Thanks.
Since the question seems a bit like homework, here's an answer that starts a bit like a lecture:
ask yourself what happens to the function as s and t go to small and to large positive and negative values; this will help you to identify the range of values you should be examining; both Mathematica and Matlab can help your figure this out;
draw the graph of your function over the range of values of interest, develop a feel for its shape and try to figure out where it has maxima; for this the Mathematic Plot3D[] function and the Matlab plot() function will both be useful;
since this is a function of 2 variables, you should think about plotting some of its sections, ie hold s (or t) constant, and make a 2D plot of the section function; again, develop some understanding of how the function behaves;
now you should be able to do some kind of search of the s,t values around the maxima of the function and get an acceptably accurate result.
If this is too difficult then you could use the Mathematica function NMaximize[]. I don't think that Matlab has the same functionality for symbolic functions built-in and you'll have to do the computations numerically but the function findmax will help.
In Matlab, one would create a vector/matrix with s and t values, and a corresponding vector with the function values. Then you can pinpoint the maximum using the function max
In Mathematica, use FindMaximum like this:
f[s_,t_]:= ((1+s)^4 + (s+t)^4 + t^4)/(1+s^4+t^4)
FindMaximum[ f[s,t],{s,0},{t,0} ]
This searches for a maximum starting from (s,t)=(0,0).
For more info, see http://reference.wolfram.com/mathematica/ref/FindMaximum.html