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:
Simple problem

Subject: Simple problem

From: Andreas

Date: 26 Nov, 2010 04:25:04

Message: 1 of 4

Hello, i have a simple problem. I say that xd(1)=u*cos(theta) but in the output it is taken as zero! What i'm doing wrong? My code is below and at the end is the output! Thanks

function [ ] = ntarki( theta, tInc )

x(1)=0;
y(1)=0;
t(1)=0;
d=1.2;
m=600;
u=900;
g=9.81;
i=1;
xd(1)=u*cos(theta)
yd(1)=u*sin(theta)

while t(i)<30

    if t(i)<15
        a = 0.25;
        cd = 0.38;
    else
        a = 1.1;
        cd = 0.9;
    end
    

Ax(i) = (tInc/2)*(-1/m)*(0.5*d*a*cd*(yd(i)^2+xd(i)^2));
Ay(i) = (tInc/2)*(-1/m)*(0.5*d*a*cd*(yd(i)^2+xd(i)^2)+m*g);
Bitax(i)=(tInc/2)*(xd(i)+Ax(i)/2);
Bitay(i)=(tInc/2)*(yd(i)+Ay(i)/2);
Bx(i) = (tInc/2)*(-1/m)*(0.5*d*a*cd*(yd(i)^2+xd(i)^2));
By(i) = (tInc/2)*(-1/m)*(0.5*d*a*cd*(yd(i)^2+xd(i)^2)+m*g);
Cx(i) = (tInc/2)*(-1/m)*(0.5*d*a*cd*(yd(i)^2+xd(i)^2));
Cy(i) = (tInc/2)*(-1/m)*(0.5*d*a*cd*(yd(i)^2+xd(i)^2)+m*g);
Deltax(i) = (tInc)*(xd(i)+Cx(i));
Deltay(i) = (tInc)*(yd(i)+Cy(i));
Dx(i) = (tInc/2)*(-1/m)*(0.5*d*a*cd*(yd(i)^2+xd(i))^2);
Dy(i) = (tInc/2)*(-1/m)*(0.5*d*a*cd*(yd(i)^2+xd(i))^2+m*g);

x(i+1)= x(i)+tInc*(xd(i)+1/3*(Ax(i)+Bx(i)+Cx(i)));
xd(i+1)=xd(i)+1/3*(Ax(i)+2*Bx(i)+2*Cx(i)+Dx(i));

y(i+1)=y(i)+tInc*(yd(i)+1/3*(Ay(i)+By(i)+Cy(i)));
yd(i+1)=yd(i)+1/3*(Ay(i)+2*By(i)+2*Cy(i)+Dy(i));

xdd(i) = -(1/m)*(0.5*d*a*cd*(yd(i)^2+xd(i)^2));
ydd(i) = -(1/m)*(0.5*d*a*cd*(yd(i)^2+xd(i)^2)+m*g);

t(i+1)=t(i)+tInc;

i=i+1;


end

----------------------------------------------------------------------------------------------------

#
///////OUTPUT FOR ntarki(0.3,0.1)

xd = 859.8028

 
yd = 265.9682

  

yd =
  1.0e+127

   Columns 1 through 16

 

    0.0000 -0.0000 -0.0000 -0.0000 -3.0914 -Inf NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN

 

xd =

 
  1.0e+127 *

 

  Columns 1 through 16


    0.0000 -0.0000 -0.0000 -0.0000 -3.0914 -Inf NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN

Subject: Simple problem

From: ImageAnalyst

Date: 26 Nov, 2010 05:29:26

Message: 2 of 4

I don't get anything like that at all. When I run your code, the only
output is this in the command window:
xd =
  859.8028
yd =
  265.9682
I'd say what is going wrong is you not using the debugger. Learning
how to use the debugger is what will allow you to solve problems like
this.

Your function also looks strange, and not just because there are no
comments in it and you're using i as a loop counter. You call it and
do a bunch of math but then don't return anything. Nothing is saved,
plotted, or returned, so what's the point?

Subject: Simple problem

From: Walter Roberson

Date: 26 Nov, 2010 09:39:07

Message: 3 of 4

On 25/11/10 10:25 PM, Andreas wrote:
> Hello, i have a simple problem. I say that xd(1)=u*cos(theta) but in the
> output it is taken as zero! What i'm doing wrong?

Are you aware that cos() works on radians? Use cosd() if you want degrees.

Subject: Simple problem

From: Roger Stafford

Date: 27 Nov, 2010 01:07:04

Message: 4 of 4

"Andreas " <pentaxxx@gmail.com> wrote in message <icncr0$1cq$1@fred.mathworks.com>...
> Hello, i have a simple problem. I say that xd(1)=u*cos(theta) but in the output it is taken as zero! What i'm doing wrong? My code is below and at the end is the output!
> .........
> xd =
> 1.0e+127 *
> Columns 1 through 16
> 0.0000 -0.0000 -0.0000 -0.0000 -3.0914 -Inf NaN NaN ....
- - - - - - - - -
  As to your question of why for xd(1) "in the output it is taken as zero!", the answer is that it is NOT zero in the output. The value of xd(1) has actually not changed at all. Notice the multiplying factor "1.0e+127 *" above the lines of output. The closest five decimal place value in x.xxxxe+127 for 859.8028 would have to be 0.0000e+127 ! You should always pay close attention to that multiplicative factor when interpreting your vector or matrix output displays.

  Of course the reason that factor is set so high in your display is that later values of xd are enormous and the factor has to be set to handle the largest of these. By the fifth one it is already up to -3.0914e+127, and at the next step it has exceeded the 'realmax' limit and become -inf. Whatever you are computing is expanding far faster than you have apparently planned for.

  Out of curiosity just what is it you are attempting to compute? That "m*g" term suggests the weight of a mass of 600 kg on the earth, but I can't understand why you multiply by yd(i)^2+xd(i)^2 at each step - some kind of force is supposed to increase in direct proportion to the square of the distance rather than inverse proportion?

Roger Stafford

Tags for this Thread

No tags are associated with 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