Is there a way to plot a line of best fit through the origin in MATLAB

205 views Asked by At

I am currently working on an engineering project and have to use complex numbers. I want to take the complex numbers, plot the real and imaginary parts of them, and then fit a line of best fit that is forced through the origin. This is the following code I have tried (using forums):

close all
clear all
clc

x = [2, -1]; % real parts
y = [1, 6]; % imaginary parts


    for i = 1 : length(x);

    plot([0, x(i)],[0, y(i)] ,'-k', 'LineWidth', 2)
    hold on

    end

hold on

plot(x,y, 'sk', 'Markersize', 10)
yline(0, ':k')
xline(0, ':k')
xlabel('Real axis')
ylabel('Imaginary axis')


x1 = linspace(-10,10,100);

a = x'\y';

y1 = a * x1;

plot(x1, y1, 'r')

For some combination of complex numbers, the line of best fit looks ideal. However, like in this case, the graph gives me the following that does not satisfy my requirement:

enter image description here

Ideally, the line should go through the middle of both of the points. Any help would be greatly appreciated. I have just realised that the reason why is because the y intercept has been 'moved' down but I am still unsure on another method.

Thanks! :)

0

There are 0 answers