Plotting several values returned by a function.

  • Thread starter Thread starter peripatein
  • Start date Start date
  • Tags Tags
    Function Plotting
AI Thread Summary
The discussion focuses on plotting the values returned by a MATLAB function that calculates the interpolation polynomial for log(x). The user initially struggles with plotting the function's output for x values ranging from 0.01 to 12. A key suggestion is to include an end statement in the function definition and to utilize a for loop to iterate through the x values, storing results in an array. The proposed loop structure allows for the collection of values to be plotted after execution. This approach ensures that the function can successfully generate and plot the desired data points.
peripatein
Messages
868
Reaction score
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
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?
 
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.
 
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.
 
Back
Top