Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
rose

Subject: rose

From: rose lans

Date: 1 Nov, 2008 18:38:02

Message: 1 of 4

Hi,
I am a beginner to MATLAB, so i am sorry if my question is way too trivial.
I am working on a program where I have to plot two variables, N and t:
function [t, N]=vloop (N,S,O)

i=1;
for i1=1:length (N)
   Ne=N (i1);
for i2=1:length (S)
   Sal=S (i2);
for i4=1:length (O)
   OvH=O(i4);
[ X, r, Y,e]=F(3, 30, Ne) ;
Nov=(X+Y);
total=total_cost (Ne,S,Nov,O);
N (i)=Ne;
Salv (i)=Sal;
OvHv (i)=OvH;
i=i+1;

end
end
end

F and total_cost are separate functions in separate m-files. I am using their output to get total (I hope that makes sense).
The program runs, but when I try to plot total vs. N, I get
??? Error using ==> plot
Vectors must be the same lengths.
How can I fix that?
Thank you.

Subject: rose

From: russell.fung@gmail.com

Date: 1 Nov, 2008 20:41:44

Message: 2 of 4

On Nov 1, 1:38=A0pm, "rose lans" <byruse...@yahoo.com> wrote:
> Hi,
> I am a beginner to MATLAB, so i am sorry if my question is way too trivia=
l.
> I am working on a program where I have to plot two variables, N and t:
> function [t, N]=3Dvloop (N,S,O)
>
> i=3D1; =A0 =A0
> for i1=3D1:length (N)
> =A0 =A0Ne=3DN (i1);
> for i2=3D1:length (S)
> =A0 =A0Sal=3DS (i2);
> for i4=3D1:length (O)
> =A0 =A0OvH=3DO(i4);
> [ X, r, Y,e]=3DF(3, 30, Ne) ; =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0=
 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0
> Nov=3D(X+Y);
> total=3Dtotal_cost (Ne,S,Nov,O);
> N (i)=3DNe;
> Salv (i)=3DSal;
> OvHv (i)=3DOvH;
> i=3Di+1;
>
> end
> end
> end
>
> F and total_cost are separate functions in separate m-files. I am using t=
heir output to get total (I hope that makes sense).
> The program runs, but when I try to plot total vs. N, I get
> ??? Error using =3D=3D> plot
> Vectors must be the same lengths.
> How can I =A0fix that?
> Thank you.

try

total(i)=3Dtotal_cost(Ne,S,Nov,O);

instead of

total=3Dtotal_cost(Ne,S,Nov,O);

Subject: rose

From: Miroslav Balda

Date: 1 Nov, 2008 21:10:04

Message: 3 of 4

"rose lans" <byrusel42@yahoo.com> wrote in message <gei7ma$id6$1@fred.mathworks.com>...
> Hi,
> I am a beginner to MATLAB, so i am sorry if my question is way too trivial.
> I am working on a program where I have to plot two variables, N and t:
> function [t, N]=vloop (N,S,O)
>
> i=1;
> for i1=1:length (N)
> Ne=N (i1);
> for i2=1:length (S)
> Sal=S (i2);
> for i4=1:length (O)
> OvH=O(i4);
> [ X, r, Y,e]=F(3, 30, Ne) ;
> Nov=(X+Y);
> total=total_cost (Ne,S,Nov,O);
> N (i)=Ne;
> Salv (i)=Sal;
> OvHv (i)=OvH;
> i=i+1;
>
> end
> end
> end
>
> F and total_cost are separate functions in separate m-files. I am using their output to get total (I hope that makes sense).
> The program runs, but when I try to plot total vs. N, I get
> ??? Error using ==> plot
> Vectors must be the same lengths.
> How can I fix that?
> Thank you.

Hi,
There are several weak points in your code:
1. If you like to plot results outside the function vloop, the output argument t should be assigned some values, it means it should be indexed inside the most inner loop.
2. The code is dangerous , because the input variable N is overwritten inside the inner loop. More over, the length of N will increase over the original length. Better would be to use different name of the output argument.
3. There are variables Salv and OvHv which are not used. More over their dimension is increasing, what means that computing time will be very long. To inprove it, you should declare them before the outer loop by assigning them, say
Salv=zeros(1,length(N)*length(S)*length(O));
Hope it helps.
Mira

Subject: rose

From: rose lans

Date: 3 Nov, 2008 00:05:04

Message: 4 of 4

"Miroslav Balda" <miroslav.nospam@balda.cz> wrote in message <geigjc$k7b$1@fred.mathworks.com>...
> "rose lans" <byrusel42@yahoo.com> wrote in message <gei7ma$id6$1@fred.mathworks.com>...
> > Hi,
> > I am a beginner to MATLAB, so i am sorry if my question is way too trivial.
> > I am working on a program where I have to plot two variables, N and t:
> > function [t, N]=vloop (N,S,O)
> >
> > i=1;
> > for i1=1:length (N)
> > Ne=N (i1);
> > for i2=1:length (S)
> > Sal=S (i2);
> > for i4=1:length (O)
> > OvH=O(i4);
> > [ X, r, Y,e]=F(3, 30, Ne) ;
> > Nov=(X+Y);
> > total=total_cost (Ne,S,Nov,O);
> > N (i)=Ne;
> > Salv (i)=Sal;
> > OvHv (i)=OvH;
> > i=i+1;
> >
> > end
> > end
> > end
> >
> > F and total_cost are separate functions in separate m-files. I am using their output to get total (I hope that makes sense).
> > The program runs, but when I try to plot total vs. N, I get
> > ??? Error using ==> plot
> > Vectors must be the same lengths.
> > How can I fix that?
> > Thank you.
>
> Hi,
> There are several weak points in your code:
> 1. If you like to plot results outside the function vloop, the output argument t should be assigned some values, it means it should be indexed inside the most inner loop.
> 2. The code is dangerous , because the input variable N is overwritten inside the inner loop. More over, the length of N will increase over the original length. Better would be to use different name of the output argument.
> 3. There are variables Salv and OvHv which are not used. More over their dimension is increasing, what means that computing time will be very long. To inprove it, you should declare them before the outer loop by assigning them, say
> Salv=zeros(1,length(N)*length(S)*length(O));
> Hope it helps.
> Mira
>
Thank you for a quick reply,
It does help, I just got confused in syntax.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us