From: Jacob on
Heya,

I've been trying for over an hour to make fplot work, however the solution eludes me.

I have genrated a few 2d points with noise on the y-coordinate, and I'm using least squares adjustment to find the line. I'd like to have both points and line plottet in the same figure.

[script]
clear all
close all
clc

%Dette script genererer data til approksimation af en linie.

x_coor = [];

for i = 1:5
x_coor = [x_coor;i];
end

for i = 1:5
y_n = x_coor + 10;
end

y = normrnd(y_n,0.7);

plot(x_coor,y,'*')

p = size(y_n,1);

A = [];

for k = 1:p
A = [A;x_coor(k) 1;];
end

b = y_n;

xhat = inv(A'*A)*A'*b;

r = A*xhat - b;

a = xhat(1)

c = xhat(2)

fplot(@(x),'a*x+c',[0 5])
[/script]

for the last line I've tried:
fplot(@(x),['a*x+c'],[0 5])
fplot('a*x+c',[0 5])
fplot(@(x),[a*x+c],[0 5]

I keep getting a parse error, an unbalanced paranthese error and a few more, no matter what I try. I also get the 'Use semicolon to supress output'-message for that line. This message remains even if I put a semicolon after the fplot command.

Best regards,
Jacob Collstrup
From: Steven Lord on

"Jacob " <jjco06(a)land.aau.dk> wrote in message
news:hrjcho$dmc$1(a)fred.mathworks.com...
> Heya,
>
> I've been trying for over an hour to make fplot work, however the solution
> eludes me.
>
> I have genrated a few 2d points with noise on the y-coordinate, and I'm
> using least squares adjustment to find the line. I'd like to have both
> points and line plottet in the same figure.

*snip*

> fplot(@(x),'a*x+c',[0 5])

The way you've written this function handle, it accepts an input and returns
the string 'a*x+c'. If you want this function handle to accept an input x
and return the _values_ computed by a*x+c, you need to remove the quotes.

fplot(@(x) a*x+c, [0 5])

Just make sure a and c are defined before you make this call to FPLOT.

--
Steve Lord
slord(a)mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ