Plotting several values returned by a function.
3 次查看(过去 30 天)
显示 更早的评论
Hi, 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.
0 个评论
回答(1 个)
Jie
2013-10-26
You could have plotted the result in a loop (if you have used the function file correctly). But I suggest u not do this.
In order to make this right and simple, change your function file into the following:
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;
only three dots were added. But now your function file is vector/matrix supported.( Your previous file is only scalar supported ) And then use this following code:
x = 0.01:0.01:12;
y=Lagrange1(x);
figure,plot(x,y)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Polynomials 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!