Plotting several values returned by a function.

In summary, the conversation discusses a function for the interpolation polynomial of log(x) and how to plot its values for a range of x values. The conversation also includes a suggested for loop to call the function and plot the values.
  • #1
peripatein
880
0
Hi,

Homework Statement


I wrote the following function for the interpolation polynomial of log(x):

function pol = Lagrange1(x)
dim1 = [1, 2, 4];
dim2 = [0, 0.693, 1.386];
pol1 = dim2(1)*(x-dim1(1))*(x-dim1(3))/[(dim1(1)-dim1(2))*(dim1(1)-dim1(3))];
pol2 = dim2(2)*(x-dim1(1))*(x-dim1(3))/[(dim1(2)-dim1(1))*(dim1(2)-dim1(3))];
pol3 = dim2(3)*(x-dim1(1))*(x-dim1(2))/[(dim1(3)-dim1(1))*(dim1(3)-dim1(2))];
pol = pol1 + pol2 + pol3;

I'd like to plot the value it returns for x = 0.01:0.01:12. How may I go about it, please? I have tried using ordinary plot(), in a loop even, to no avail.


Homework Equations





The Attempt at a Solution

 
Physics news on Phys.org
  • #2
You're going to have to provide more details. What software were you using to plot? What happened when you tried to plot? What does x = 0.01:0.01:12 mean?
 
  • #3
Sorry about that. I am trying to code this using MATLAB. 0.01:0.01:12 means that x varies between 0.01 and 12 in intervals of 0.01.
 
  • #4
peripatein said:
Hi,

Homework Statement


I wrote the following function for the interpolation polynomial of log(x):

function pol = Lagrange1(x)
dim1 = [1, 2, 4];
dim2 = [0, 0.693, 1.386];
pol1 = dim2(1)*(x-dim1(1))*(x-dim1(3))/[(dim1(1)-dim1(2))*(dim1(1)-dim1(3))];
pol2 = dim2(2)*(x-dim1(1))*(x-dim1(3))/[(dim1(2)-dim1(1))*(dim1(2)-dim1(3))];
pol3 = dim2(3)*(x-dim1(1))*(x-dim1(2))/[(dim1(3)-dim1(1))*(dim1(3)-dim1(2))];
pol = pol1 + pol2 + pol3;

I'd like to plot the value it returns for x = 0.01:0.01:12. How may I go about it, please? I have tried using ordinary plot(), in a loop even, to no avail.
Your function definition is missing an end statement at its end.

You need to call your Lagrange function in a for loop, something like this:
Code:
j = 0
for x = .01 : .01 : 12.0
   j += 1
   plotVal(j) = Lagrange(x)
end
This loop should run 1200 times, once for each value of x from 0.01 to 12.0, in increments of 0.01. After the loop runs, plot the values in what I'm calling the plotVal array.
 
  • #5


Hello,

Thank you for sharing your function with us. Plotting the values returned by a function can be done using the "plot" function in MATLAB. In your case, you can use the following code to plot the values returned by your function for x = 0.01:0.01:12:

x = 0.01:0.01:12; % create a vector of x values
y = Lagrange1(x); % use your function to calculate the corresponding y values
plot(x,y) % plot the x and y values

If this does not work, please provide more information on the error you are getting so I can assist you further. Additionally, you may want to check that your function is returning the correct values for x = 0.01:0.01:12 before attempting to plot them. I hope this helps. Let me know if you have any further questions.
 

FAQ: Plotting several values returned by a function.

1.

What is plotting in scientific research?

Plotting is a method used in scientific research to visually represent data and relationships between variables. It involves creating a graph or chart that displays the values of a function or experiment in a clear and concise manner.

2.

Why is plotting important in scientific research?

Plotting is important in scientific research because it allows researchers to analyze and interpret data more easily. By visually representing data, patterns and trends can be identified, making it easier to draw conclusions and make predictions.

3.

What are the different types of plots used in scientific research?

The most commonly used types of plots in scientific research include line graphs, bar graphs, scatter plots, and histograms. Each type of plot is used to represent different types of data and relationships between variables.

4.

How do you plot several values returned by a function?

To plot several values returned by a function, you will need to first determine the independent and dependent variables. Then, choose an appropriate type of plot based on the data and relationships being represented. Use the values returned by the function as data points on the plot, and label and format the axes accordingly.

5.

What are some tips for creating effective plots in scientific research?

Some tips for creating effective plots in scientific research include choosing the right type of plot for the data, labeling all axes with clear and concise titles, using appropriate scales and units, and including a legend or key if necessary. It is also important to ensure that the plot is easy to read and understand for the intended audience.

Similar threads

Back
Top