2

I have two array 451x1 , I want to fit a line to a part of my data, for x=3.8 –4.1 , and I want to evaluate interception of fitted line with line y=0, Do you have any idea? (In matlab ) enter image description here

data

Abolfazl
  • 453
  • 4
  • 8
  • 19

3 Answers3

3

You can easily perform a linear regression by indexing the points of the curve you want to use and passing them to the function POLYFIT. Here's the code to do it and a plot of the fit line:

index = (x >= 3.8) & (x <= 4.1);   %# Get the index of the line segment
p = polyfit(x(index),y(index),1);  %# Fit polynomial coefficients for line
yfit = p(2)+x.*p(1);  %# Compute the best-fit line
plot(x,y);            %# Plot the data
hold on;              %# Add to the plot
plot(x,yfit,'r');     %# Plot the best-fit line
axis([1 7 0 4e10]);   %# Adjust the axes limits

enter image description here

Then you can compute where this line intercepts the x-axis (i.e. y = 0) like so:

>> -p(2)/p(1)

ans =

    3.5264
gnovice
  • 125,304
  • 15
  • 256
  • 359
1

I'm just guessing that your question is to estimate a line with a zero y-intercept, although honestly, "want to evaluate interception of fitted line with line y=0" makes little sense in English to me. So this is just a complete guess, unless you choose to clarify your question.

  1. Delete that part of the data that does NOT lie in the interval of interest. (Or if you prefer, extract only that part which does.)

  2. Fit a line with zero y-intercept to the data of interest.

    slope = x(:)\y(:);

  • Ok, I also plot that part of plot again on the previous line, then I used basic fitting on tools from figure menu. Ialso used curv fitting from “Microcal Origin”. But when I have many plots I try to use she shortes way. – Abolfazl Apr 23 '11 at 19:30
1

You would fit x respect to y like:

> ndx= find(x>= 3.8& x<= 4.1);
> b= [y(ndx) ones(size(ndx))]\ x(ndx)

Now b(2) is "the interception with" line y= 0.

eat
  • 7,440
  • 1
  • 19
  • 27
  • I think it has a problem, I tried it severeal times, but it doesn't work. – Abolfazl Apr 24 '11 at 15:21
  • 1
    @Abolfazl: Can you be more specific. What `doesn't work`? Please, note that `x= y.* b(1)+ b(2)` and thus `b(2)` is the requested interception. Thanks – eat Apr 25 '11 at 09:22