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); ''
Información
La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.
Mostrar comentarios más antiguos
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
Respuestas (1)
Walter Roberson
el 31 de Ag. de 2015
Editada: Walter Roberson
el 31 de Ag. de 2015
0 votos
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.
4 comentarios
Avan Al-Saffar
el 31 de Ag. de 2015
Editada: Avan Al-Saffar
el 31 de Ag. de 2015
Walter Roberson
el 31 de Ag. de 2015
You should ask yourself whether you should really be using integrate(). It looks to me as if you have a specific set of points that you want to calculate the area over, instead of having a function with a variable. If you have a set of points to integrate look at trapz()
Avan Al-Saffar
el 31 de Ag. de 2015
Walter Roberson
el 31 de Ag. de 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
La pregunta está cerrada.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!