# How to add trendline to semilog plot in matlab

• MATLAB
• adeeyo
In summary, the conversation involves a request for help with creating a semilogx plot in MATLAB, adding a trendline, and obtaining the slope. The necessary code is provided and the trendline can be found by searching online.
adeeyo
Hi,
I have x and y vectors, x=[ 90868 68151 45434 34076 27261 13631 6816 3408 2273 1948 1705 1137 853 683 569 455 342 274 228 190]; y=[ 3680 3723 3800 3866 3920 4103 4250 4320 4340 4344 4350 4364 4373 4379 4384 4393 4398 4402 4405 4407];
I need to plot semilogx in MATLAB and add trendline and get the slope as shown in the image attached and also shown in this link http://img708.imageshack.us/img708/7717/0056005c.jpg
Please how do I go about it?

Thanks

adeeyo

#### Attachments

• 00560_05c.jpg
26.7 KB · Views: 1,100
Last edited by a moderator:
Hey adeeyo.

In MATLAB, you should type or enter the following to produce the semilogx plot:

x = [ 90868 68151 45434 34076 27261 13631 6816 3408 2273 1948 1705 1137 853 683 569 455 342 274 228 190];
y=[ 3680 3723 3800 3866 3920 4103 4250 4320 4340 4344 4350 4364 4373 4379 4384 4393 4398 4402 4405 4407];

h = semilogx(x,y,'*','color','black')
xlabel('Horner Time Function - Tp=13630.0');
ylabel('Pressure (psia)');
title('Ex. 5-1: Semilog (Horner) Plot');

I have to run for now but the trendline should be straightforward if you just google "matlab trendline"

Cheers

## 1. How do I create a semilog plot in MATLAB?

To create a semilog plot in MATLAB, you can use the semilogy function. This function takes two input arguments: the x-values and the y-values. Simply pass in your data and the plot will automatically be displayed in a semilog scale.

## 2. How can I add a trendline to my semilog plot in MATLAB?

To add a trendline to your semilog plot, you can use the polyfit and polyval functions. First, use polyfit to calculate the coefficients of the polynomial that best fits your data. Then, use polyval to evaluate the polynomial at different x-values and plot it on your semilog plot.

## 3. Can I customize the appearance of the trendline on my semilog plot?

Yes, you can customize the appearance of the trendline by using additional input arguments in the polyfit and polyval functions. For example, you can specify the line style, color, and width of the trendline. You can also add a legend to your plot to label the trendline.

## 4. How can I add multiple trendlines to my semilog plot?

To add multiple trendlines to your semilog plot, you can use the hold on command to prevent the plot from being cleared after each polyval function call. This will allow you to plot multiple trendlines on the same plot. Remember to use hold off when you are finished plotting your data.

## 5. Is there a way to automatically calculate and add a trendline to my semilog plot in MATLAB?

Yes, you can use the plotfit function to automatically calculate and add a trendline to your semilog plot. This function takes the same input arguments as polyfit and polyval, but it will also plot the trendline for you. However, note that this function may not always produce the most accurate trendline for your data, so it is recommended to manually calculate the trendline using polyfit and polyval.