In an assignment A(I) = B, the number of elements in B and I must be the same. Error in odearguments (line 88) f0 = feval(ode,​t0,y0,args​{:}); % ODE15I sets args{1} to yp0. Error in ode45 (line 114) [neq, tspan, ntspan...

1 visualizzazione (ultimi 30 giorni)
Alright. First of all I must say I'm quite new in the world of Matlab. I've done a few things but they were not a very big deal. At university we've been asked to represent the animation of a bouy depending on its density and size. Here's the two pieces of code I've written:
Here I define the differential equation that describes the general position of the object
function dy=fboya(t,y)
global g l rho0 rho_a radio;
v_boya=pi*radio^2*l;
m_boya= rho0*v_boya;
P=m_boya*g;
E=(rho_a*v_boya*g).*(z(1)<-l/2)+(rho_a*pi*radio^2.*(l/2-z(1))*g).*((-l/2<z(1))&(z(1)<l/2))+(0).*(l/2<z(1));
dz=zeros(2,1);
dz(1)=z(2);
dz(2)=(E-P)/m_boya;
And here is the rest of the code:
function [t,y]=mboya2(L,y_0,v_0,rho_agua,rho_0,tf,R)
global g l rho0 rho_a radio;
g=9.81; % gravedad
l=L;
rho0=rho_0; % densidad del objeto
rho_a=rho_agua; % densidad del agua
radio=R;
[t,y]=ode45(@fboya2,[0,tf],[y_0,v_0]);
y=y(:,1);
x=0;
close all;
figure;
set(gcf(),'Position',[300 250 800 300]);
subplot(1,2,1)
plot(x(1),y(1),'or',[0 x(1)],[0 y(1)],'b')
axis([-l,l,-l,l]);
axis square;
The thing is that I keep getting this error messages and I don't really undersand why is this happening.
In an assignment A(I) = B, the number of elements in B and I must be the same.
Error in fboya2 (line 14) dy(2)=(E-P)/m_boya; Error in odearguments (line 88) f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode45 (line 114) [neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs, odeFcn, ...
Error in mboya2 (line 20) [t,y]=ode45(@fboya2,[0,tf],[y_0,v_0]);

Risposta accettata

Walter Roberson
Walter Roberson il 12 Dic 2013
Have another look at
E=(rho_a*m_boya).*(y<-l/2)+(rho_a*pi*radio^2.*(l/2-y)).*((-l/2<y)&(y<l/2))+(0).*(l/2<y);
Your "y" has multiple elements, so (y<-1/2) has multiple elements, so E is going to have multiple elements.
Then in
dy(2)=(E-P)/m_boya;
since your E has multiple elements (and your m_boya is scalar), the expression on the right hand side has multiple elements. But you are trying to store those multiple elements into the single location dy(2)
  7 Commenti
Walter Roberson
Walter Roberson il 13 Dic 2013
You define py=z(:,1); so py is a column vector whose length is the number of rows in z. The number of rows is the same as size(z,1) so zeros(1,size(z,1)) is a row vector with the same number of elements as the column vector py.
If you wanted x to be a column vector instead of a row vector, you could instead use
x = zeros(size(py));
and Yes for a row vector you could use
x = zeros(size(py)).' ;
if you wanted.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Programming in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by