how to make function

2 Ansichten (letzte 30 Tage)
poppy am 1 Dez. 2022
Bearbeitet: DGM am 1 Dez. 2022
I have this given function
𝑓(𝒙)=(𝑥1^2+𝑥2 −2)^2+(𝑥1 +𝑥2^2−4)^2+𝑥1*𝑥2
I need to make a function that computes that value of f(x*) at the point x*[1 1], how would i go about doing this?
1 Kommentar-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden
Stephen23 am 1 Dez. 2022
Bearbeitet: Stephen23 am 1 Dez. 2022
how to make function
I have this given function
𝑓(𝒙)=(𝑥1^2+𝑥2 −2)^2+(𝑥1 +𝑥2^2−4)^2+𝑥1*𝑥2
I need to make a function that computes that value of f(x*) at the point x*[1 1], how would i go about doing this?

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Walter Roberson am 1 Dez. 2022
Example:
syms x1 x2
x = [x1;x2]
x =
f(x) = (x1^3 - 2*x2^2 + 3*x1*x2 - 5)^3 + x1*x2
f(x1, x2) =
xstar = x * [-5 9]
xstar =
f(xstar(1,:), xstar(2,:))
ans =
Reminder: if x is a 1 x 2 vector instead of a 2 x 1 vector, then x * [1 1] would be a (1 x 2) inner product with (1 x 2) and that would fail because the * inner product operation requires that the number of columns of the first operand be the same as the number of rows of the second operand. So [1 x 2] * [1 x 2] would fail. And that tells us that x must be [x1; x2] in a column, a 2 x 1 -- but then x * [1 1] gives a 2 x 2 result
When you try to define a symbolic function with a vector of variables, like in this case, MATLAB permits the syntax of assigning to f(x) to create the function, but the function that is created will have two parameters not one. That is why the xstar value has to be split into x1 and x2 portions to pass it in to the function. Should the dividing have been done by rows or by columns? I'll leave that to you to think about?
0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Programming finden Sie in Help Center und File Exchange

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by