The result is not what I want it is completely different ,,so Is there anyone could help me to use another routines to get the result for this code please?

This code what I am doing,, My problem is in plot(4),The result is not what I want it is completely different ,,so Is there anyone could help me to use another routines to get the result for this code please?
function Runfisher
A=1;
b=3;
x0=[12 5];
dt=0.01;
tspan=0:dt:1;
[t,x]=ode45(@fisher,tspan,x0,[],b);
figure(1)
plot(t,x)
figure(2)
plot(x(:,1),x(:,2),'-')
grid
dt= 0.095;
a1 = ( 15*x(1) - b*x(1).*x(2) );
b1 = ( -5*x(2) + 0.5*x(1).*x(2) );
I = A.*(dt.^2).*sum.*( ( ( ( (a1.*(15-b*x(2))+b1.*(.5*x(2))).*a1 ) + ( (a1.*(-b*x(1))+ b1.*(-5+.5*x(1))).*b1) ).^2)./(( (a1.^2) + (b1.^2) ).^2) )
ball=2:1:10 ;
n = length(ball) ;
Iall = zeros(1,n) ;
for i = 1 : n
[t,x]=ode45(@fisher,(0:0.0001:1),x0,[],ball(i)) ;
figure(3)
plot(x(:,1),x(:,2))
hold on
dt= 0.095;
a1 = ( 15*x(1) - ball(i).*x(1).*x(2) );
b1 = ( -5*x(2) + 0.5*x(1).*x(2) );
I = A.*(dt.^2).*sum.*( ( ( ( (a1.*(15-ball(i).*x(2))+b1.*(.5*x(2))).*a1 ) + ( (a1.*(-ball(i).*x(1))+ b1.*(-5+.5*x(1))).*b1) ).^2)./(( (a1.^2) + (b1.^2) ).^2) ) ;
Iall(i) = I
end
figure(4)
plot(ball,Iall,'r-')
1;
% function dxdt = fisher(t,x,b)
% dxdt=zeros(2,1);
% dxdt(1) = 15 * x(1) - b * x(1).* x(2);
% dxdt(2) = -5 * x(2) + .5 * x(1).* x(2);
% end
The right plot for figure(4) should increase until (ball =3 ) then it will deceasing until the last value. But what I am getting is just increasing line . I do not what is the reason so could anyone help me please?

13 Comments

Questions:
1. What is the value of the variable "sum" in the calculation of I ?
2. x is an (nx2) matrix where n is the number of output times from ODE45.
So what are x(1) and x(2) in the calculation of I ?
3. Why is the function "fisher" commented out ?
Best wishes
Torsten.
Many thanks for your concern.
According to your questions, I noticed that may be I am not using the correct routine so please I will answer your quires and if you can help me to solve my problem:
1- the value of the '' sum '' is from 1 to n ( the number of output from ODE45), This mean that I need to get the result of summation .
2- May be I could not understand your question well but My systems contains two equations with two variables x(1) and x(2) which they represent the Predator-Prey variables and they are depending on time .
3- I just referred it out here to informed you what is the system that I am calling in my ODE45
ad 1) "sum" is a MATLAB operator that sums elements of arrays or matrices.
So "sum.*" has to be replaced simply by "sum".
ad 2)
I guess "I" should contain an approximation of an integral over time.
Thus you will have to replace
x(1) by x(:,1),
x(2) by x(:,2),
a1 = ( 15*x(1) - ball(i).*x(1).*x(2) ); by a1(:) = ( 15*x(:,1) - ball(i)*x(:,1).*x(:,2) );
and
b1 = ( -5*x(2) + 0.5*x(1).*x(2) ); by b1(:) = ( -5*x(:,2) + 0.5*x(:,1).*x(:,2) );
Of course I don't know whether the function to be integrated is correct and whether the approximation of the integral by your sum is correct.
Best wishes
Torsten.
Many thanks for you Torsten.
I did what you mentioned but I received this error message :
In an assignment A(:) = B, the number of elements in A and B must be the same.
Error in Runsystem (line 41)
a1(:) = ( 15*x(:,1) - ball(i)*x(:,1).*x(:,2) );
I don't know why, but the setting of a1, b1 and I appear twice in your code (at the beginning and in the for-loop). Furthermore, since tspan changes, the dimensions of a1 and b1 will be different at both positions of your code. So the easiest way is to start your code from the line
ball=2:1:10;
and delete the upper part.
Best wishes
Torsten.
They appear twice because for the first time I am fixing the value of (b) and later I am changing the value of (b) from 2 to 10. I will try your suggestion.
Many thanks Regards
I still have the same plot even I applied your suggestion.
Could you show the code you are using now ?
Best wishes
Torsten.
function Runsystemvaryb
A=1;
x0=[12 5];
ball=2:1:10 ;
n = length(ball) ;
Iall = zeros(1,n) ;
for i = 1 : n
[t,x]=ode45(@system,(0:0.0001:1),x0,[],ball(i)) ;
figure(1)
plot(x(:,1),x(:,2))
hold on
dt= 0.055;
% a1 = ( 15*x(1) - ball(i).*x(1).*x(2) );
% a1(:) = ( 15*x(:,1) - ball(i).*x(:,1).*x(:,2) );
% b1 = ( -5*x(2) + 0.5*x(1).*x(2) );
% b1(:) = ( -5*x(:,2) + 0.5*x(:,1).*x(:,2) );
% I = A.*(dt.^2).*sum( ( ( ( (a1.*(15-ball(i)*x(2))+b1.*(.5*x(2))).*a1 ) + ( (a1.*(-ball(i)*x(1))+ b1.*(-5+.5*x(1))).*b1) ).^2)./(( (a1.^2) + (b1.^2) ).^2) )
I = A.*(dt.^2).*sum( ( ( ( (( 15*x(:,1) - ball(i).*x(:,1).*x(:,2) ).*(15-ball(i).*x(:,2))+( -5*x(:,2) + 0.5*x(:,1).*x(:,2) ).*(.5*x(:,2))).*( 15*x(:,1) - ball(i).*x(:,1).*x(:,2) ) ) + ( (( 15*x(:,1) - ball(i).*x(:,1).*x(:,2) ).*(-ball(i).*x(:,1))+ ( -5*x(:,2) + 0.5*x(:,1).*x(:,2) ).*(-5+.5*x(:,1))).*( -5*x(:,2) + 0.5*x(:,1).*x(:,2) )) ).^2)./(( (( 15*x(:,1) - ball(i).*x(:,1).*x(:,2) ).^2) + (( -5*x(:,2) + 0.5*x(:,1).*x(:,2) ).^2) ).^2) )
Iall(i) = I./10000
end
figure(2)
plot(ball,Iall,'r-')
1;
% function dxdt = system(t,x,b)
% dxdt=zeros(2,1);
% dxdt(1) = 15 * x(1) - b * x(1).* x(2);
% dxdt(2) = -5 * x(2) + .5 * x(1).* x(2);
% end
I get the attached output for Iall.
Best wishes
Torsten.
With an analogue Fortran-program, I get the following output for Iall:
ball Iall
2.00000000000000 1.65285396632246
3.00000000000000 2.03520968157525
4.00000000000000 3.46914314988618
5.00000000000000 6.11508746765178
6.00000000000000 9.82670128843249
7.00000000000000 14.5325924533958
8.00000000000000 20.1308960907625
9.00000000000000 25.9469750868376
10.0000000000000 31.4212066754488
Best wishes
Torsten.
Sorry, the numbers on the right should be one order of magnitude smaller (I divided by 1000, not by 10000).
Best wishes
Torsten.
Dear Torsten
Yes, it is still increasing but what I am looking for is increasing curve until b=3 which will give us the higher value and then will decreasing until the last value which is b=10.
Many thanks for your help . I believe that I should re-understand what I am doing again and if you have any idea please post it to me here.
Regards Avan

Answers (0)

This question is closed.

Asked:

on 4 Nov 2014

Closed:

on 20 Aug 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!