1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Plotting several values returned by a function.

  1. Oct 25, 2013 #1
    1. The problem statement, all variables and given/known data
    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.

    2. Relevant equations

    3. The attempt at a solution
  2. jcsd
  3. Oct 25, 2013 #2


    User Avatar
    Staff Emeritus
    Science Advisor
    Homework Helper

    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?
  4. Oct 25, 2013 #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.
  5. Oct 25, 2013 #4


    Staff: Mentor

    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 (Text):
    j = 0
    for x = .01 : .01 : 12.0
       j += 1
       plotVal(j) = Lagrange(x)
    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.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted