Filter löschen
Filter löschen

Unrecognized function or variable 'x' (best approximation)

2 Ansichten (letzte 30 Tage)
Bayro97
Bayro97 am 23 Apr. 2021
Kommentiert: Walter Roberson am 24 Apr. 2021
This is my first Matlab programme. It is supposed to determine the best approximation of a function. Unfortunately, it only spits out error messages. One that I simply cannot solve is:
Unrecognized function or variable 'x'.
This appears with function phi = create_phi(n).
PROGRAM:
syms x
N = 3;
f = @(x) cos(x);
p = pi
u = bestapproximation(f,0,N,[0,p]);
function phi = create_phi(n)
phi = zeros(n);
for i=0:n
phi(i) = x.^i;
end
end
function G = create_Gram(phi,n,I)
G = zeros(n,n);
for i=0:n
for j=0:n
G(i,j) = integral(phi(i)*phi(j),I(0),I(1));
end
end
end
function f_vector = create_f_vector(f,phi,n,I)
f_vector = zeros(n);
for i=0:n
f_vector(i) = integral(phi(i)*f,I(0),I(1));
end
end
function u = bestapproximation(f,y,n,I)
phi = create_phi(n);
G = create_Gram(phi,n,I);
f_vector = create_f_vector(f-y,phi,n,I);
u = G \ f_vector;
end

Antworten (1)

Walter Roberson
Walter Roberson am 23 Apr. 2021
Solving the problem of x not being defined, but not solving the next problem:
mech204_hw6
p = 3.1416
Array indices must be positive integers or logical values.

Error in solution>mech204_hw6/create_phi (line 12)
phi(i) = x.^i;

Error in solution>mech204_hw6/bestapproximation (line 33)
phi = create_phi(n);

Error in solution>mech204_hw6 (line 7)
u = bestapproximation(f,0,N,[0,p]);
function mech204_hw6
x = sym('x');
N = 3;
f = @(x) cos(x);
p = pi
u = bestapproximation(f,0,N,[0,p]);
function phi = create_phi(n)
phi = zeros(n);
for i=0:n
phi(i) = x.^i;
end
end
function G = create_Gram(phi,n,I)
G = zeros(n,n);
for i=0:n
for j=0:n
G(i,j) = integral(phi(i)*phi(j),I(0),I(1));
end
end
end
function f_vector = create_f_vector(f,phi,n,I)
f_vector = zeros(n);
for i=0:n
f_vector(i) = integral(phi(i)*f,I(0),I(1));
end
end
function u = bestapproximation(f,y,n,I)
phi = create_phi(n);
G = create_Gram(phi,n,I);
f_vector = create_f_vector(f-y,phi,n,I);
u = G \ f_vector;
end
end
  2 Kommentare
Bayro97
Bayro97 am 23 Apr. 2021
I was able to solve the problem. Now would like to be defined differently... Matlab is quite bitchy. Python is a bit more accessible.
function mech204_hw6
x = sym('x');
f = @(x) cos(x);
p = pi;
I = [0,p];
e = zeros(9,1);
for N=2:10
u = bestapproximation(f,0,N,I);
e(N) = cal_failure(f,u,phi,I);
end
plot(N,e)
function phi = create_phi_poly(n)
phi = [];
phi.append(@(x) 1);
for k=2:n
phi.append(@(x) x.^k);
end
end
function G = create_Gram(phi,n,I)
G = zeros(n,n);
for i=1:n
for j=1:n
G(i,j) = integral(@(x) phi(i)*phi(j),I(1),I(2));
end
end
end
function f_vector = create_f_vector(f,phi,n,I)
f_vector = zeros(n);
for i=1:n
f_vector(i) = integral(@(x) phi(i)*f,I(1),I(2));
end
end
function u = bestapproximation(f,y,n,I)
phi = create_phi_poly(n);
G = create_Gram(phi,n,I);
f_vector = create_f_vector(f-y,phi,n,I);
u = G \ f_vector;
end
function e = cal_failure(f,u,phi,I)
f_approx = @(x) u * phi;
e = sqrt(integral(@(x) f_approx - f, I(1), I(2)));
end
Walter Roberson
Walter Roberson am 24 Apr. 2021
e(N) = cal_failure(f,u,phi,I);
You have not defined phi.
phi = [];
Inside that function, you initialize phi to the empty double precision array
phi.append(@(x) 1);
append is not a method that is defined for double precision arrays.
Note: You are creating function handles, and function handles cannot be stored in double precision arrays. You need to store them in cell arrays.
function u = bestapproximation(f,y,n,I)
phi = create_phi_poly(n);
You create phi there, but you do not return it from the function.
f = @(x) cos(x);
Okay, f is a function handle.
u = bestapproximation(f,0,N,I);
and you pass it to bestapproximation, passing 0 as the second parameter
Inside bestapproximation you do
f_vector = create_f_vector(f-y,phi,n,I);
which takes the function handle and attempts to subtract y, which is 0 in this case. But you cannot subtract from a function handle.
What datatype does create_f_vector expect?
function f_vector = create_f_vector(f,phi,n,I)
f_vector(i) = integral(@(x) phi(i)*f,I(1),I(2));
phi is trying to be a vector of function handles. You cannot use () indexing with function handles if you want to extract a particular handle. This is because MATLAB permits functions to be called without any parameters, so if you hypothetically had
phi = [@sin, @cos] %not actually valid
then if you did phi(1) MATLAB would not be able to tell whether you intend to extract the @sin handle, or you instead intended to evaluate each of the function handles with parameter 1, getting back [sin(1), cos(1)]. You need to use a cell array,
phi = {@sin, @cos}
and then phi(1) would be cell indexing giving back the cell {@sin} and phi{1} would extract content, giving you back @sin (without the containing cell), so phi{1}(1) would evaluate sin(1) without any ambiguity.
Assume for a moment that you have converted phi to cell, so you had
f_vector(i) = integral(@(x) phi{i}*f,I(1),I(2));
phi{i} would be a function handle, and f would be.. we are not sure at the moment what it is intended to be.
If f (which is f-y currently where f is a function handle) is intended to be a function handle, then phi{i}*f would be trying to take two function handle multiplied by each other, but multiplication is not defined for function handles. And if multiplication were defined for function handles, you would have the problem that phi{i}*f ignores the variable x in @(x) phi{i}*f context, so your integral could not work.
If f (which is f-y currently where f is a function handle) is intended to have evaluated to a numeric constant at that point (that is, f-y had been f(something)-y so that subtract could work), then integral(@(x) phi{i}*f,I(1),I(2)) would be attempting to multiply the function handle phi{i} by a constant, which again is not going to work. And again, you are not using x so your integral cannot work.
When you have a function handle, you need to pass a value to it if you want the handle to be evaluated.

Melden Sie sich an, um zu kommentieren.

Tags

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by