fplot doesnt give the same result of plot

조회 수: 2 (최근 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?
N/A
N/A 2019년 5월 13일
this is the data file with the inputs

댓글을 달려면 로그인하십시오.

답변 (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