fplot doesnt give the same result of plot
7 views (last 30 days)
Show older comments
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 Comments
Jan
on 13 May 2019
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?
Answers (2)
dpb
on 13 May 2019
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 Comments
dpb
on 13 May 2019
Edited: dpb
on 13 May 2019
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
on 13 May 2019
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 Comment
dpb
on 13 May 2019
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.
See Also
Categories
Find more on Visual Exploration in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!