Calculating value of function in point

110 visualizaciones (últimos 30 días)
Ds31
Ds31 el 23 de Oct. de 2021
Comentada: Matt J el 23 de Oct. de 2021
I have to write Matlab function:function value = evaluate(f,x,y,n) that evaluates value of real function f on equidistant array of n points on segment [x,y]. Function has to return vector of dimension 2xn, such that in first row are points from equidistant array, and in second row is function value in those points.
Here is my try, but this code doesn't seem to work. I get errors when I run this code. Any help is appreciated.
f=@(x)2*x+2^x;
test = evaluate(f,0,6,7);
function value = evaluate(f,x,y,n)
a = linspace(x,y,n);
b = f(a(1:n));
matrix = [a:b];
end

Respuestas (1)

Matt J
Matt J el 23 de Oct. de 2021
f=@(x)2*x+2.^x;
test = evaluate(f,0,6,7)
test = 2×7
0 1 2 3 4 5 6 1 4 8 14 24 42 76
function matrix = evaluate(f,x,y,n)
a = linspace(x,y,n);
b = f(a);
matrix = [a;b];
end
  2 comentarios
Ds31
Ds31 el 23 de Oct. de 2021
So what was mistake in my code?
Matt J
Matt J el 23 de Oct. de 2021
  1. 2.^x instead of 2^x
  2. [a;b] instead of [a:b]
  3. The return argument needs to be "matrix" instead of "value"

Iniciar sesión para comentar.

Etiquetas

Productos


Versión

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by