# Taking data from matrix and using it in a function

조회 수: 3(최근 30일)
enter 1 Apr 2020
댓글: enter 1 Apr 2020
Let's denote . I would like to find the maximum value of the function z, if I substitute with -1 or 1. I created a matrix of only -1 and 1 using de2bi function (I got 0's and 1's but substituted 0's with -1's). How can I "connect" the matrix with my function, so that I'll check all possible combinations and get the maximum value?

로그인 to comment.

### 답변(1개)

Birdman 1 Apr 2020
편집: Birdman 1 Apr 2020
As far as I understand, you want to change E1, E2, E3 and E4 respectively with combinations of -1 and 1. So the following code does that:
syms z(x,y) E1 E2 E3 E4
z(x,y)=(((sin(x).*(1+E1))./(3))+(1+E2))./(log10(y+E3)-E4);
A=perms([-1 1 -1 1]);
for i=1:size(A,1)
Z(x,y)=subs(z,{E1,E2,E3,E4},{A(i,1),A(i,2),A(i,3),A(i,4)})
end
After this point, you can proceed. If you have further question, we can discuss from here. Hope this gives the idea.

#### 댓글 수: 3

enter 1 Apr 2020
I got a function calculating maximum :
x = linspace(1,10,10e3);
y=x;
[X,Y] = meshgrid(x,y);
z=(((sin(x).*(1+E1))./(3))+(1+E2))./(log10(y+E3)-E4);
Z_max = max(max(z(X,Y)))
fsurf(z,[1,10,1,10])
How to implement it into your code? Every time I try I get this error :
Error using sym/subsasgn (line 959)
Invalid indexing or function definition. Indexing must follow MATLAB indexing. Function arguments must be symbolic variables, and
function body must be sym expression.
Birdman 1 Apr 2020
Try the following code. Note that I had to set x as
x = linspace(1,10,100);
to reduce the calculation time. The max values are stored in
Z_max
syms z(x,y) E1 E2 E3 E4
z(x,y)=(((sin(x).*(1+E1))./(3))+(1+E2))./(log10(y+E3)-E4);
A=perms([-1 1 -1 1]);
c = linspace(1,10,100);
d=c;
[C,D] = meshgrid(c,d);
for i=1:length(A)
Z(x,y)=subs(z,{E1,E2,E3,E4},{A(i,1),A(i,2),A(i,3),A(i,4)});
Z_max(i) = max(max(double(vpa(Z(C,D),2))));
figure(i);
fsurf(Z,[1,10,1,10])
end
The code takes some time to be calculated.
enter 1 Apr 2020
Is there a more efficient way, because for this code it'll take lifetime to compile.
syms z(x,y) E1 E2 E3 E4 E5 E6 E7 E8 E9
z(x,y) = E7+E8-E9-E3.*((y-1.0)./(y-log(y))+(y.*sin((y.*pi)./1.8e+2))./(cos((y.*pi)./1.8e+2)+x.^2.*3.0))+(E6.*log(y))./(y-log(y))+(E4.*cos((y.*pi)./1.8e+2))./(cos((y.*pi)./1.8e+2)+x.^2.*3.0)+(E5.*cos((y.*pi)./1.8e+2))./(cos((y.*pi)./1.8e+2)+x.^2.*3.0)+(E1.*x.^2.*6.0)./(cos((y.*pi)./1.8e+2)+x.^2.*3.0)+(E2.*x.^2.*3.0)./(cos((y.*pi)./1.8e+2)+x.^2.*3.0)
;
A=perms([-1 1 -1 1 -1 1 -1 1 -1 1]);
c = linspace(1,10,100);
d=c;
[C,D] = meshgrid(c,d);
for i=1:length(A)
Z(x,y)=subs(z,{E1,E2,E3,E4,E5,E6,E7,E8,E9},{A(i,1),A(i,2),A(i,3),A(i,4),A(i,5),A(i,6),A(i,7),A(i,8),A(i,9)});
Z_max(i) = max(max(double(vpa(Z(C,D),2))));
end

로그인 to comment.

이 질문에 답변하려면 로그인을(를) 수행하십시오.

Translated by