How can i create a multivariable function from matrix data?

Hi i´m new to matlab. Im trying to create a program that reads data out of a matrix and assigns those values to variables. the issue is that this must be a function for which im going to evaluate its minima, so i cannot define p.
My problem is of this kind:
im going to evaluate the minimum value possible of p
A=[150;
200;
300;]
I want to generate this from the previous data:
f(1)=A(1,1)*p(1)
f(2)=A(2,1)*p(2)
f(3)=A(3,1)*p(3)
resulting at the end that
Totalcost= f(1)+f(2)+f(3); which will a multivariable function.
I tried to use a for loop in this case but it doesnt let me assign the values in the parenthesis to the p, to make it a different variable. it keeps telling me that i´m reffering to a value that has been cleared.
what i want to know is: which commands can i use to assign values to the p as a variable of a multivariable function?

1 件のコメント

Natanael Acencio Rijo
Natanael Acencio Rijo 2013 年 12 月 4 日
編集済み: Natanael Acencio Rijo 2013 年 12 月 4 日
I finally got the result for the function to minimize, but it´s still in sym class, so it doesnt let me evaluate it for optimization. but it doesnt let me convert the variables p to double class, so that the optimization algorithm can input values in it.

サインインしてコメントする。

 採用された回答

sixwwwwww
sixwwwwww 2013 年 12 月 4 日
編集済み: sixwwwwww 2013 年 12 月 4 日

0 投票

if you have symbolic toolbox installed then you can do it as follows:
A = [150; 200; 300];
p = sym('p%d', [1 numel(A)]);
for i = 1:numel(A)
f(i) = A(i) * p(i);
end
Totalcost = sum(f(:));
disp(Totalcost)

7 件のコメント

Natanael Acencio Rijo
Natanael Acencio Rijo 2013 年 12 月 4 日
編集済み: Natanael Acencio Rijo 2013 年 12 月 4 日
I finally got the result for the function to minimize, but it´s still in sym class, so it doesnt let me evaluate it for optimization. but it doesnt let me convert the variables p to double class, so that the optimization algorithm can input values in it.
sixwwwwww
sixwwwwww 2013 年 12 月 4 日
You can use
double(YourSymbolicValue)
and it will convert the symbolic value into double type value which you can use further in your code. See for more information:
Natanael Acencio Rijo
Natanael Acencio Rijo 2013 年 12 月 4 日
I tried that already it keeps telling me this:
The following error occurred converting from sym to double: Error using mupadmex Error in MuPAD command: DOUBLE cannot convert the input expression into a double array. If the input expression contains a symbolic variable, use the VPA function instead.
using VPA just deals with the coefficients. Hopefully I´ll get outta this. Thanks..
sixwwwwww
sixwwwwww 2013 年 12 月 4 日
can you show me your code? to properly figure out
Natanael Acencio Rijo
Natanael Acencio Rijo 2013 年 12 月 4 日
function cost= cost(p) ;
clc
clear
costfunctionsetting;
sizel=size(CostFormulavariables);
alpha=CostFormulavariables(:,1);
beta=CostFormulavariables(:,2);
gamma=CostFormulavariables(:,3);
syms p(k)
for k = 1:sizel;
f(k)=(alpha(k,1)+(beta(k,1)*p(k))+(gamma(k,1)*p(k)^2));
end
cost=vpa(sum(f))
end
Natanael Acencio Rijo
Natanael Acencio Rijo 2013 年 12 月 4 日
ans =
5.3*p(1) + 5.5*p(2) + 5.8*p(3) + 0.004*p(1)^2 + 0.006*p(2)^2 + 0.009*p(3)^2 + 1100.0
this is my final result, what i want to do then is being able to find its minimum, but it is in sym class and the optimization toolbox doesnt accept the variables.
sixwwwwww
sixwwwwww 2013 年 12 月 4 日
you need to subsitutute values for symbols p(1), p(2), ... then you can get numeric values in symbols which you can convert to double type using doouble()

サインインしてコメントする。

その他の回答 (1 件)

Community Treasure Hunt

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

Start Hunting!

Translated by