if cycle is not working
    1 vue (au cours des 30 derniers jours)
  
       Afficher commentaires plus anciens
    
    DDD
 le 2 Mai 2015
  
    
    
    
    
    Réponse apportée : Image Analyst
      
      
 le 2 Mai 2015
            Hello! while solving an exercise the if cycle is not working. Is anything wrong? Thanks
L=1; xfinal=L;
u=[];
x_inicial=0; dx=xfinal/20;
x=x_inicial:dx:xfinal;
t=0; dt=0.0001; t_max=1.1;
c=zeros(1,length(x));
c(((xfinal/dx)/2+1))=1;
while t<t_max
    t=t+dt;
    for w=2:(length(x)-1)
        c(w)=c(w)+dt*((c(w+1)+c(w-1)-2*c(w))/(dx^2));
        c(1)=0; c(length(x))=0;
    end
    if t==0.01||t==0.1||t==1
        figure()
        plot(x,c)
        title(sprintf('L=%d and t_f=%1.1f',xfinal,t))
    end
end
0 commentaires
Réponse acceptée
  Image Analyst
      
      
 le 2 Mai 2015
        Yes, something is wrong. You're comparing floating point numbers for equality. See the FAQ for workarounds and the proper way to do it: http://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F
0 commentaires
Plus de réponses (0)
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

