fplot doesnt give the same result of plot

3 次查看(过去 30 天)
N/A
N/A 2019-5-13
评论: dpb 2019-5-13
when i try to convert this code:
data = csvread('Lab6Data.txt');
x = data(1,:);
y = data(2,:);
plot(x,y,'.r')
for n = 1:3
poly = polyfit(x,y,n+1);
polyFunc = @(poly) polyval(poly,x);
polyY = polyFunc(poly)
plot(x,polyY);
end
xlabel('x')
ylabel('y')
title('plot1')
legend('data','deg-2','deg-3','deg-4')
to an fplot like this
data = csvread('Lab6Data.txt');
x = data(1,:);
y = data(2,:);
plot(x,y,'.r')
for n = 1:3
poly = polyfit(x,y,n+1);
polyFunc = @(poly) polyval(poly,x);
polyY = polyFunc(poly)
fplot(polyFunc);
end
xlabel('x')
ylabel('y')
title('plot1')
legend('data','deg-2','deg-3','deg-4')
it doesnt look like the regular plot, which is what i need
  2 个评论
Jan
Jan 2019-5-13
Without your inputs, we cannot run your code. So all we currently know is "it doesnt look like the regular plot". This is not enough to understand, what your problem is. What exactly do you need? Why do you want to use fplot, if plot works as you want?

请先登录,再进行评论。

回答(2 个)

dpb
dpb 2019-5-13
So, why not just use plot if that's what you want?
But, read the doc for fplot -- all is explained:
Description
...
fplot(f) plots the curve defined by the function y = f(x) over the default interval [-5 5] for x.
fplot(f,xinterval) plots over the specified interval. Specify the interval as a two-element vector of
the form [xmin xmax].
fplot(___,LineSpec) specifies the line style, marker symbol, and line color. '-r' plots a red line.
Use this option after any of the input argument combinations in the previous syntaxes.
fplot(___,Name,Value) specifies line properties using one or more name-value pair arguments.
...
  2 个评论
N/A
N/A 2019-5-13
the assignment requires the use of fplot, not plot
dpb
dpb 2019-5-13
编辑:dpb 2019-5-13
Well, one would presume then the way fplot looks is the way the result is supposed to look?
Or, if you were given a specific format to reproduce, either use the optional arguments or, of course, you can save the FunctionLine object returned by fplot and modify the line characteristics through it.
The figure/axes are standard figures and axes, you can retrieve the handles to them and modify any other properties desired; x/ylim etc., etc., work just the same as well...all fplot is is an attempt at a figure from a function handle that you don't have to mess with further but can be manipulated like any other as wish...or the instructor demands! :)

请先登录,再进行评论。


Star Strider
Star Strider 2019-5-13
In your fplot loop, you may not be using your ‘polyFunc’ function correctly. It will work best with ‘x’ as the argument, not ‘poly’:
hold all
plot(x,y,'.r')
for n = 1:3
poly = polyfit(x,y,n+1);
polyFunc = @(x) polyval(poly,x);
polyY = polyFunc(x)
fplot(polyFunc, [min(x) max(x)]);
end
hold off
xlabel('x')
ylabel('y')
title('plot1')
legend('data','deg-2','deg-3','deg-4')
If your assignment tells you to do otherwise, this obviously wil not work.
  1 个评论
dpb
dpb 2019-5-13
Results-wise, I don't see there should be any difference, Star.
His functional embeds the value of the invariant X variable and passes the variable fitted coefficents; yours embeds the current set of coefficients and passes X, but the result will be the same numerically.
I'd tend to write with both as arguments, but "the way Matlab works" with functional definitions embedding variables in the current workspace inside the definition that aren't arguments the end result here is the same.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Surface and Mesh Plots 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by