Plotting several values returned by a function.

  • Thread starter Thread starter peripatein
  • Start date Start date
  • Tags Tags
    Function Plotting
Click For Summary

Discussion Overview

The discussion revolves around a homework problem involving the implementation of a Lagrange interpolation polynomial for the logarithm function in MATLAB. Participants are seeking assistance with plotting the values returned by the function over a specified range of x values.

Discussion Character

  • Homework-related
  • Technical explanation

Main Points Raised

  • One participant shares a MATLAB function for the Lagrange interpolation polynomial of log(x) and requests help with plotting its output for a range of x values.
  • Another participant asks for clarification on the software being used and the meaning of the specified range for x.
  • A later reply suggests that the function definition is missing an end statement and proposes a for loop to call the Lagrange function for each value of x, accumulating results in an array for plotting.

Areas of Agreement / Disagreement

Participants have not reached a consensus on the plotting method, and there are differing views on the completeness of the original function definition.

Contextual Notes

There is a mention of a missing end statement in the function definition, which may affect its execution. The discussion also highlights the need for clarification on the plotting approach and the structure of the loop for accumulating values.

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.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
1K
Replies
4
Views
1K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
2
Views
4K