Matlab using forward euler method - matlab-figure

dt/dx=x(1.15-0.02x-0.02y)
dt/dy=y(1.34-0.02y-0.04x)
T=15;
dt=1;
N=floor(T/dt);
t(1)=1;
x(1)=21;
y(1)=20;
for i=1:N
x(i+1)=x(i)+dt*x(i)*(1.15-0.02*x(i)-0.02*y(i));
y(i+1)=y(i)+dt*y(i)*(1.34-0.02*y(i)-0.04*x(i));
t(i+1)=i*dt;
end
title('state variable')
subplot(2,1,1)
plot(t,x,'r*-')
subplot(2,1,2)
plot(t,y,'bo-')
Using forward euler method
It is correct?

I think so but you have a mistake in Your equation it shoul be
dx/dt=...
dy/dt=...

Related

Julia multiply 2 matrices in a for loop with a formula

Hello hopefully someone can help me... I am absolutely new to programming.
I just want to multiply two matrices in Julia with a for loop. It is about the frequency of a oscillation table at continuous casting in a steel plant. The below function calculates the frequency (=freqres) for a given mold stroke and casting speed(vc). The result is the frequency of the oscillation table for a given casting speed (from 1000mm/min to 6000mm/min).
vc = [1000:100:6000;]
function freqcalc(vc,stroke)
for i in vc
push!(freqres, 3i/4stroke)
end
end
Now I want to calculate the negative strip time with the following formula:
nst = (60/(pi*freqres))*acos(vc/(pi*stroke*freqres))
With the below command I did not achieve the desired result, I think due to a nested condition.
nstres = Float64[]
for i in vc, j in freqres
push!(nstres, (60/(pi*j))*acos(i/(pi*stroke*j)))
end
I just want to have the result of every entry of vc and freqres of the above formula in nstres.
push!(nstres, (60/(pi*freqres1))*acos(vc1/(pi*stroke*freqres1)))
push!(nstres, (60/(pi*freqres2))*acos(vc2/(pi*stroke*freqres2)))
I hope I could clarify my question, and thanks for your comments, I very appreciate it.

Displaying results of solve function in TI-89 programming (cubic equation)

I have a program to find principal stresses. In it, is a cubic equation that I would like to solve and display 3 results. Here are my questions:
1. is solve() function correct one to use in ti-basic to solve the cubic equation? if not, which is correct?
2. When executing the program in home screen how to display three values of cubic equation?
Here is my program:
princstr(xx,yy,zz,xy,xz,yz)
Prgm
a = xx+yy+zz
b = xx*yy+yy*zz+zz*xx-xy^2-yz^2+xz^2
c = xx*yy*zz+xy*yz*xz+xz*xy*yz-xz*yy*xz-xz*yy*xz-yz*yz*xx-zz*xy*xy
solve(s^3-a*s^2+b*s-c=0,s)
Return s
EndPrgm
In the home screen it just returs
Done
Thank you in advance!
P.s. Edits are welcome.
You are not displaying S. Return does not work like return in other languages. In TI-Basic, Return does not return a value; it return control to the calling program. If you want to display it, you should do
:Disp S
:Return

Code running very slow

My code seems to run very slowly and I can't think of any way to make it faster. All my arrays have been preallocated. S is a large number of element (say 10000 element, for example). I know my code runs slowly because of the "for k=1:S" but i cant think of another way to perform this loop at a relatively fast speed. Can i please get help because it takes hours to run.
[M,~] = size(Sample2000_X);
[N,~] = size(Sample2000_Y);
[S,~] = size(Prediction_Point);
% Speed Preallocation
Distance = zeros(M,N);
Distance_Prediction = zeros(M,1);
for k=1:S
for i=1:M
for j=1:N
Distance(i,j) = sqrt(power((Sample2000_X(i)-Sample2000_X(j)),2)+power((Sample2000_Y(i)-Sample2000_Y(j)),2));
end
Distance_Prediction(i,1) = sqrt(power((Prediction_Point(k,1)-Sample2000_X(i)),2)+power((Prediction_Point(k,2)-Sample2000_Y(i)),2));
end
end
Thanks.
I realized the major problem was organization of my code. I was performing calculation in a loop where it was absolutely unnecessary. So i seperated the code in two blocks and it Works much faster.
for i=1:M
for j=1:N
Distance(i,j) = sqrt(power((Sample2000_X(i)-Sample2000_X(j)),2)+power((Sample2000_Y(i)-Sample2000_Y(j)),2));
end
end
for k=1:S
for i=1:M
Distance_Prediction(i,1) = sqrt(power((Prediction_Point(k,1)-Sample2000_X(i)),2)+power((Prediction_Point(k,2)-Sample2000_Y(i)),2));
end
end
Thanks to the community for the help.
Your matrix Distance does not depend on k, so you can easily calculate it outside the main for-loop, for instance using:
d = sqrt((repmat(Sample2000_X, [1,M]) - repmat(Sample2000_X', [M,1])).^2 + (repmat(Sample2000_Y, [1,N]) - repmat(Sample2000_Y', [N,1])).^2);
I assume M=N, because elsewise your code won't work. Next, you can calculate your Distance_Prediction matrix. It is rather strange that you calculate this inside the for-loop over k, because the matrix will be changed in every iteration without using it. Anyway, this will do exactly the same as your code:
for k=1:S
Distance_Prediction = sqrt((Sample2000_X - Prediction_Point(k,1)).^2 + (Sample2000_Y - Prediction_Point(k,1)).^2);
end

Write matrix with Fortran

I need to output a matrix with FORTRAN. I have a working code that calculates the values, but instead of a matrix, I get single a column. The matrix is huge, ixj = ~2000x2000.
Here is my sample code:
open(19, file="results1.txt", status="old", position="rewind",
& action="write")
do j=0,p
do i=0,o
write(19,*) mat_user_yield_surface(d, eps(i), deps(j), 200.0d0)
end do
end do
close(19)
Use an implied do loop:
do j=0,p
write(19,'(2000g22.14)') (mat_user_yield_surface(d, eps(i), deps(j),200.0d0),i=0,o)
end do
I suggest not using "o" as a variable name, since it is easily confused with zero.
This "write(19,'(2000g22.14)')" worked perfectly! Thanks. So the final code is:
open(19, file="results1.txt", status="old", position="rewind",
& action="write")
do j=0,p
write(19,'(2000g22.14)') (mat_user_yield_surface(d, eps(i),
& deps(j), 200.0d0), i=0,o)
end do
close(19)

What causes "Jacobian matrix" to be singular in SAS?

I have a simple SAS (version 9.2) program as follows,
proc model;
cdf('normal',log(V/100)+1)=0.5;
bounds V>0;
solve V/solveprint;
run;
It throws exception that says jacobian matrix to be singular,
The Newton method Jacobian matrix of partial derivatives of the
equations with respect to the variables to be solved is singular.
What is the possible cause of this error?
Update: I have simplified the problem a bit. When modified to "cdf('normal', X)=0.5", it works without exception.
Update2: bounds is updated to V>0; but exception still there
What input data set are you passing to proc model? For example, this code works consistently:
data a;
v=100;
run;
proc model data=a;
cdf('normal',log(V/100)+1) = 0.5;
bounds V>0;
solve V / solveprint;
run;
quit;
And gives a solution of V=36.78794
But changing the input data somewhat (see below) will consistently give a singular Jacobian matrix error.
data a;
v=0.00001;
run;
proc model data=a;
cdf('normal',log(V/100)+1) = 0.5;
bounds V>0;
solve V / solveprint;
run;
quit;
You are asking SAS to solve a function that has no solution. You are asking for the value of V>1000 that makes this equation true. But there are no such values because log(1000/100+1) is about 3.3, and the CDF of a Normal random variable with mean 0 and standard deviation 1 evaluated at 3.3 is 0.9995. Any larger value of V will just move the function closer to 1, not toward 0.5, so there is no answer to your question.
By telling you that the matrix of partial derivatives is singular, SAS is just using fancy math speak for "your function doesn't have a solution". (Really what it's saying is, "I've turned your question into an equivalent maximization problem, and that problem doesn't have a maximum, so I can't help you.")

Resources