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...

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]);

 Accepted Answer

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 Comments

I've also tried at defining E only as :
E=(rho_a*pi*radio^2.*(l/2-y));
but I still get the same errors. Nevertheless, the point of this is that E is kind of an step function and is thought to be defined as it is. Thanks for your reply though.
Your definition of E still involves the vector y. Your original expression for E had 5 "y" in it, so unless you were to extract exactly one element from y, E is going to end up as a vector.
E might be a step function, but when you apply it to the multidimensional point "y", the result needs to be a scalar.
Alright, I've changed both codes a bit in order to avoid using y as a vector.This is the first piece of code:
function dz=fboya(t,z)
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 this is the other. Here I've also changed the definition of x making it a vector of zeros as the buoy is only supposed to be moving in the 'y' direction. The only change you may notice is the axis' limits. Since this code now executes, I've modified that bit in order to have a better looking animation:
function [t,x,y]=mboya(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,z]=ode45(@fboya,[0,tf],[y_0,v_0]);
py=z(:,1);
y=py;
x=zeros(1,size(z));
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,min(min(py))-2,max(max(py))+2]);
axis square;
subplot(1,2,2);
plot(t,py,t(1),py(1),'or');
ylabel('y(t)');
xlabel('t');
for i=2:length(t)
subplot(1,2,1)
plot(x(i),y(i),'or',[0 x(i)],[0 y(i)],'b')
axis([-l,l,min(min(py))-2,max(max(py))+2]);
axis square;
subplot(1,2,2);
plot(t,py,t(i),py(i),'or');
ylabel('y(t)');
xlabel('t');
pause(0.1);
end
The only problem is that I get the following warning:
Warning: Input arguments must be scalar.
> In mboya at 18
I've tried changing x to
x=zeros
or
x=0
but if I leave it that way, the animation doesn't work.
Thanks for the help. I just need that little thing solved.
x=zeros(1,size(z)); is the line at fault. size(z) returns a vector, so you have x=zeros(1,a vector) which is not allowed. If you want x to be the same size as z then
x = zeros(size(z));
and if you want x to be a vector with the same length as py, then
x = zeros(1, size(z,1));
Is it correct that you want all of your x to be 0 ?
First of all thanks for all your help =). I guess it must be quite tough going through all that code. I did what you said and now it's perfectly working. Nevertheless, I have a question about what you said:
The first one would create a vector x with the size of z, including both the rows and columns and making them 0. But wouldn't the second one be a bit redundant?, 'cause it's actually like taking the numbers of rows in a vector ( which is obviously 1) and then making a 1 by 1 vector, thus being the same as writing
x=0
or
x = zeros(size(py));
Is that right?
PS: yeah, I want x to be all zeroes as I don't intend to vary it's position. It would be nicer if I could actually draw the cylindrical buoy, but I don't really know how to do that, or even if it's possible or not.
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.
Alright, I think I got it. Thank you very much for your help.

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!