【问题标题】:Draw log graph curve on Matlab by clicking?通过点击在Matlab上绘制对数图曲线?
【发布时间】:2014-04-17 09:27:51
【问题描述】:

我想通过在 X-Y 平面上单击我希望它通过的点在空(半对数)图上绘制一条曲线。

有这个功能吗?

编辑:我正在尝试通过获取最后一个指针单击的位置来做到这一点-

axis([0 3000 0 1000]); 
co=get(gcf, 'CurrentPoint'); 

好像是返回执行时的光标位置,但是后面没有变化。

edit2:这对我有用。我可以使用收集的点数组进行实际绘图。

clear
clc
h=plot(0);
grid on;

xlim([0 3000]);
ylim([0 1000]);
datacursormode on;

% Enlarge figure to full screen.
screenSize = get(0,'ScreenSize');
set(gcf, 'units','pixels','outerposition', screenSize);
hold on;

% Print the x,y coordinates - will be in plot coordinates
x=zeros(1,10); y=zeros(1,10);
for p=1:10;

[x(p),y(p)] = ginput(1) ;

% Mark where they clicked with a cross.
plot(x(p),y(p), 'r+', 'MarkerSize', 20, 'LineWidth', 3);

% Print coordinates on the plot.
label = sprintf('(%.1f, %.1f)', x(p), y(p));
text(x(p)+20, y(p), label);

end

【问题讨论】:

    标签: matlab graph coordinates draw curve


    【解决方案1】:

    不是真的,但现在有:

    function topLevel
    
        %// parameters
        xrange = [0 100];
        yrange = [1e-4 1e4];
    
        %// initialize figure, plot    
        figure, clf, hold on
        plot(NaN, NaN);
        axis([xrange yrange]);
        set(gca, 'YScale', 'log')
        t = text(sum(xrange)/2, sum(yrange)/2, ...
            '<< Need at least 3 points >>',...
            'HorizontalAlignment', 'center');
    
        %// Main loop
        xs = [];  p = [];
        ys = [];  P = [];
        while true
    
            %// Get new user-input, and collect all of them in a list
            [x,y] = ginput(1);
            xs = [xs; x]; %#ok<AGROW>
            ys = [ys; y]; %#ok<AGROW>
    
            %// Plot the selected points
            if ishandle(p)
                delete(p); end        
            p = plot(xs, ys, 'rx');
            axis([xrange yrange]);
    
            %// Fit curve through user-injected points
            if numel(xs) >= 3
    
                if ishandle(t)
                    delete(t); end
    
                %// Get parameters of best-fit in a least-squares sense
                [A,B,C] = fitExponential(xs,ys);
    
                %// Plot the new curve
                xp = linspace(xrange(1), xrange(end), 100);
                yp = A + B*exp(C*xp);            
                if ishandle(P)
                    delete(P); end
                P = plot(xp,yp, 'b');            
    
            end               
        end
    
        %// Fit a model of the form  y = A + B·exp(C·x)  to data [x,y]
        function [A, B, C] = fitExponential(x,y)
    
            options = optimset(...
                'maxfunevals', inf);
    
            A = fminsearch(@lsq, 0, options);
            [~,B,C] = lsq(A);
    
            function [val, B,C] = lsq(A)
    
                params = [ones(size(x(:))) x(:)] \ log(abs(y-A));
    
                B = exp(params(1));
                C = params(2);
    
                val = sum((y - A - B*exp(C*x)).^2);
    
            end
    
        end
    
    end
    

    请注意,与往常一样,拟合指数曲线可能很棘手;对于较高的数据值,模型和数据之间的差的平方比较低的数据值要大得多,因此将有很强的偏差来更好地拟合较高的值而不是较低的值。

    我只是假设了一个简单的模型并使用了一个简单的解决方案,但这会给出一个有偏差的曲线,从您需要的意义上说,它可能不是“最佳”的。任何体面的解决方案真的取决于你具体想要什么,我将把它留给你^_^

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-20
      • 1970-01-01
      • 2020-11-25
      • 1970-01-01
      • 2014-03-15
      • 2016-12-26
      • 1970-01-01
      • 2015-11-20
      相关资源
      最近更新 更多