0
votes

Im trying to plot this function in matlab for theta from 0 to 3. Im completly new in matlab. I made 2 scripts.

Pattern

First with symsum:

syms theta
u = [2,1,-1];
y = [3,2,1];

for theta = 0 : 0.1 : 2 
    Q(theta) = symsum((y(n) + u(n)*theta)^2,n,1,3);
end

plot(theta,Q(theta);

Error: Invalid indexing or function definition.

Second with symfun

for theta = 0 : 0.1 : 2 
   Q = symfun((3-2*theta)^2 + (2-theta)^2 + (1+theta)^2, [theta]);
end
plot(Q(theta), theta);

Error: y.vars = validateArgNames(inputs);

1

1 Answers

2
votes

I you just want to plot the function then following should work

theta = 0 : 0.1 : 2 ;
Q = (3-2*theta).^2 + (2-theta).^2 + (1+theta).^2;
plot(Q,theta)

If you want to take y and u as parameters, you could also do the following

y = [3,2,1];
u = [2,1,-1];
theta = 0:0.1:2;
Q = zeros(size(theta));
for i = 1:length(y)
   Q = Q +  (y(i) - u(i).*theta).^2;
end
plot(Q,theta);