Results 1 to 2 of 2

Thread: how to plot curves with complex numbers?

  1. #1
    Join Date
    Nov 2009
    Posts
    94
    Thanks
    14
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question how to plot curves with complex numbers?

    Hello,

    I have ploted the same functions in MATLAB and then with QwtPlot and the results are different. Could you tell me why and how to deal properly with complex numbers in QwtPlot?

    Thank you

    best regards,

    Vitali

    here is the definition of functions:
    Qt Code:
    1. complex<double> benchfunc_2_constr_1(complex<double> x){
    2. return (5.0 + sqrt(100.0 - pow(x - 5.0 ,2))); // (-pow(x1-5,2) - pow(x2-5,2) +100) <=0
    3. } // resp. y >= 5 + sqrt(100 - pow(x - 5,2))
    4.  
    5. complex<double> benchfunc_2_constr_2(complex<double> x){
    6. return (5.0 + sqrt(82.81 - pow(x - 6.0 ,2))); // (-pow(x1-6,2) - pow(x2-5,2) - 82.81) <=0
    7. } // resp. y <= 5 + sqrt(82,81 - pow(x - 6,2))
    To copy to clipboard, switch view to plain text mode 

    here is the class which generates data for ploting the curves:
    Qt Code:
    1. class SimpleData: public QwtData
    2. {
    3.  
    4. public:
    5. SimpleData( complex<double> (*b)(complex<double>), double rang1[2], double rang2[2], double step)
    6. {
    7. constraintFunc = b;
    8. _step = step;
    9. rangeX[0]= rang1[0]; rangeX[1] = rang1[1];
    10. rangeY[0]= rang2[0]; rangeY[1] = rang2[1];
    11. range = (rangeX[1] - rangeX[0])/step;
    12. if(range<0){
    13. range = range *(-1);
    14. }
    15. d_size = size_t(range);
    16. }
    17.  
    18. virtual QwtData *copy() const
    19. {
    20. return new SimpleData(*this);
    21. }
    22.  
    23. virtual size_t size() const
    24. {
    25. return d_size;
    26. }
    27.  
    28. virtual double x(size_t i) const
    29. {
    30. // here the array with x values will be produced
    31. return rangeX[0] + i*((rangeX[1]- rangeX[0])/range);
    32. }
    33.  
    34. virtual double y(size_t i) const
    35. {
    36. complex<double> result = (*constraintFunc)(x(i)); // compute the y value
    37.  
    38. if(real(result) > rangeY[1]){ // if exceeds the max bound
    39. return rangeY[1];
    40. }
    41. else if(real(result) < rangeY[0]){ // if exceeds the min bound
    42. return rangeY[0];
    43. }
    44. else return real(result); // return only the real value of complex number
    45. }
    46. private:
    47. size_t d_size;
    48. double range;
    49. double _step;
    50. complex<double> (*constraintFunc)(complex<double>); // function pointer for constraint function
    51. double rangeX[2];
    52. double rangeY[2];
    53. };
    To copy to clipboard, switch view to plain text mode 

    somewhere in code the data for the curve is set with:
    Qt Code:
    1. curve1->setData(SimpleData((*ii).second, rangex1, rangex2, 0.05));
    To copy to clipboard, switch view to plain text mode 

    and here the MATLAB code for comparison:

    Qt Code:
    1. [x1, x2] = meshgrid(13:1:100, 0:1:100);
    2. f = (x1 -10).^3 + (x2 - 20).^3; % problem function
    3. g1 = @(x) sqrt(100 - (x-5).^2 ) + 5; % constraint function 1
    4. g2 = @(x) sqrt(82.81 -(x-6).^2 ) +5; % constraint function 2
    5.  
    6. t=13:0.1:100;
    7. y1 = g1(t);
    8. y2 = g2(t);
    9.  
    10.  
    11. figure
    12. subplot(1,2,1)
    13. surf(x1,x2,f);
    14. subplot(1,2,2)
    15. contour(x1,x2,f);
    16. hold on
    17. plot(t, g1(t), 'r');
    18. plot(t, g2(t), 'y');
    19. hold off
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Nov 2009
    Posts
    94
    Thanks
    14
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: how to plot curves with complex numbers?

    to define how the curves are "different" in MATLAB and QwtPlot: they look very similar if you not zoom in, but if you do, then you can see the difference, namely the curves intersect differently at x1 about 14...

Similar Threads

  1. Replies: 1
    Last Post: 11th September 2008, 09:07
  2. Complex Numbers
    By Max Yaffe in forum Qt Programming
    Replies: 2
    Last Post: 24th May 2007, 17:40

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.