Info

# Can anyone help me with this error please? '' In an assignment A(I) = B, the number of elements in B and I must be the same. Error in RunFib (line 21) JTime(j) = integral(f​,0,t(1,j),​'Waypoints​',t)./ t(1,j); ''

1 view (last 30 days)
Avan Al-Saffar on 31 Aug 2015
Closed: MATLAB Answer Bot on 20 Aug 2021
My code is :
function RunFib
x0=[12 5];
b = 3 ;
tspan = 0 : 0.001 : 10 ;
t0 =tspan(1);
tf=tspan(length(tspan));
A=1/(tf-t0);
[t,x]=ode45(@Fib, tspan , x0 , [] , 3);
a1 = ( 15*x(:,1) - 3.*x(:,1).*x(:,2) );
b1 = ( -5*x(:,2) + 0.5*x(:,1).*x(:,2) );
f = @(tspan) ( A.* (a1.* (a1 .* (15 - 3.*x(:,2)) + b1 .* (0.5 .* x(:,2)) ) + b1 .*
( a1 .* (-3 .* x(:,1)) + b1 .* (-5 + 0.5.* x(:,1)) ) ).^2 ./ (a1.^2 + b1.^2).^3 );
t= (0:0.5:10);
N = length(t);
JTime = zeros(1,N);
for j = 1:N
JTime(j) = integral(f,0,t(1,j),'Waypoints',t)./ t(1,j);
end
figure(2)
semilogy(t,JTime,'r+-')
1;
function dx = Fib(t,x,b)
dx=zeros(2,1);
dx0 = 15 * x(1) - b * x(1).* x(2);
dx1 = -5 * x(2) + 0.5 * x(1).* x(2);
end

Walter Roberson on 31 Aug 2015
Edited: Walter Roberson on 31 Aug 2015
Your f takes an input and ignores it and returns something that is size(x,1) elements. The integral of this is probably not going to be a single value.
Walter Roberson on 31 Aug 2015
You are currently doing the equivalent of
f = @(x) [17 9]
which is to say your function f ignores its input and always returns the same thing, a list of values. What are you expecting that the result would be?
Also, you currently have
function dx = Fib(t,x,b)
dx=zeros(2,1);
dx0 = 15 * x(1) - b * x(1).* x(2);
dx1 = -5 * x(2) + 0.5 * x(1).* x(2);
end
That routine computes dx0 and dx1 but then it throws them away and returns the zeros(2,1) from the
dx=zeros(2,1);
You need
function dx = Fib(t,x,b)
dx0 = 15 * x(1) - b * x(1).* x(2);
dx1 = -5 * x(2) + 0.5 * x(1).* x(2);
dx = [dx0; dx1];
end

### Community Treasure Hunt

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

Start Hunting!