Matlab: Find a root of y-values given by a differential equation

后端 未结 2 580
伪装坚强ぢ
伪装坚强ぢ 2020-12-12 01:21

I\'ve solved an initial value differential equation and plotted the Y values given by ODE45. From the plot I can vaguely tell where the root should be, but in the given task

2条回答
  •  我在风中等你
    2020-12-12 02:13

    Given a vector of Y values (ordered in the sense that the corresponding X values are steadily increasing), you may find easily near which X values the roots are located. The roots are either where a Y value is zero or between two consecutive Y values that change sign. The idea is illustrated in this code snippet:

    X = -1:0.1:1;
    Y = X.*X - 0.4;
    
    root_exact_pos  = find(Y==0);
    root_approx_pos = find(diff(sign(Y))~=0);
    

    The roots are in the X values, either in X(root_exact_pos(k)), or between X(root_approx_pos(k)) and X(root_approx_pos(k)+1), k going from 1 to the number of elements of the respective root position array.

    From here on you may apply whatever interpolation you'd like to find a better approximation of the root (I would go with linear, between the 2 points).

提交回复
热议问题