Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations cowski on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

simple solution 1

Status
Not open for further replies.

MachinaMan

Mechanical
Joined
Aug 30, 2003
Messages
70
Location
CA
why does this only give me one point on the graph ?
Code:
t=0;
x=0;

for t=0:0.01:1,
     x=(exp(-t)).*((sin(sqrt(3)*t)+cos(sqrt(3)*t)));
     t=t+0.01;
end
for t=1.01:0.01:5,
    x=(exp(-t)).*((sin(sqrt(3)*t) + cos(sqrt(3)*t))) + (0.866*exp(t-1)).*(sin(sqrt(3)*t));
    t=t+0.01;
end

plot(t,x)
thanks
Ren
 
I think you need to read up on Matlab. You really have got yourself in a muddle.

Because you are always storing the value of t and x in the same place t and x get overwritten with the new value each time you go through the loop.

Matlab is designed specifically so you don't have to write this sort of code.

your code should read
Code:
t1=[0:0.01:1];
x1=(exp(-t1)).*((sin(sqrt(3)*t1)+cos(sqrt(3)*t1)));

t2=[1.01:0.01:5];
x2=(exp(-t2)).*((sin(sqrt(3)*t2) + cos(sqrt(3)*t2))) + (0.866*exp(t2-1)).*(sin(sqrt(3)*t2));

t = [t1 t2];
x = [x1 x2];

clear x1 x2 t1 t2
    
plot(t,x)

--
Dr Michael F Platten
 
Hi MikeyP,

Thanks for your help, i had figured it out in the end. I don't use loops that often (and when I do, I don't need to)

My solution was a little different but yours is better.

Anyways thanks for the help, star for you.

René
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top