Making a generalized script, very new MATLAB user!

Hi, I am a very new to MATLAB and I used python and C a lot for scientific computing. My colleagues encouraged me to get familiar with MATLAB and its powerful symbolic functions, so I have started using it. Anyway, I made a script for some arbitrary purpose and just thinking how to make a generalized one. Here is my code:
syms x1 x2 x3 a;
f=sym('x1^2+x2^2');
point=[1,1];
variable1 = [diff(f,x1),diff(f,x2)];
variable10 = double(subs(variable1,[x1,x2],point));
d=variable1;
variable3=subs(f,[x1,x2],point+a*d);
b=12.56
variable4 = double(subs(variable3,[x1,x2,a],[point,b]));
Here I have written f of only two variables and wish to make it generalized for n variable. Is there any method exists in MATLAB how can I do this? Any ideas or suggestions? Any help much appreciated. Thanks.

Answers (2)

You can use matlabFunction() together with cell array expansion and num2cell
ThisCell = {'%08f', pi};
fprintf(ThisCell{:})
is equivalent to
fprintf('%08f', pi)
Dear Andrew, one way to generalize the code is as follows:
syms a
n = 20; % Number of variables for function f
x = sym('x%d', [1 n]);
f = sum(x.^2);
point = ones(1, n);
variable1 = [];
for i = 1:n
variable1 = [variable1 diff(f, x(i))];
end
variable10 = double(subs(variable1,x,point));
d = variable1;
variable3 = subs(f, x ,point + a * d);
b=12.56;
variable4 = double(subs(variable3,[x, a],[point, b]));
I hope it helps. Good luck!

4 Comments

Thank you very much for your Insights. I have some questions. I modified your script a bit to add more stuff. However, I got errors during program running. I thought that I can give whatever function 'f' needed, so I created a function f of 4 variables and made num (n in your case) as 4. I cannot get rid of errors. Can you present some insights? Here is my code:
f=sym('x1^2+x2^2+x3^2+x4^2');
point=[1,1,1,1];
num=4;
syms a
tolxy=1e-2;
n=1;
valxy=10;
x = sym('x%d', [1 n]);
%f = sum(x.^2);
%point = ones(1, num);
variable1 = [];
while(valxy>tolxy)
for i = 1:num
variable1 = [variable1 diff(f, x(i))];
end
variable2 = double(subs(variable1,x,point));
n=n+1;
valxy=norm(variable2);
d = -1*variable1;
variable3 = subs(f, x ,point + a * d);
f2=matlabFunction(variable3);
k=k+1;
b=12.56;
variable4 = double(subs(variable3,[x, a],[point, b]));
end
Thanks.
Dear Andrew, I pointed out some errors which you may think about again:
1) n = 1; and x = sym('x%d', [1 n]);
% These code lines are wrong in your case because it will give you error in the line variable1 == [variable1 diff(f, x(i))]; because you trying to access element x(2) which doesn't exist because of n = 1
2) variable1 = [];
% Putting this line out of while loop will cause error because in the second loop iteration the size of variable1 will be double and thus you will get error in later use of variable1
3) while valxy > tolxy
% while loop condition valxy > tolxy will generate an infinite loop because this condition never become false because valxy = norm(variable2); is always norm of same value and thus nothing is changing
4) f2=matlabFunction(variable3);
% I don't know what do you mean by this line. Are you trying to access some user defined function here?
5) k = k + 1;
% k is not defined before so you can't increment an un-defined variable
Maybe you can re-think about these errors and you will solve your problem. Good luck!
Thank you again for your help! I worked on your explanations, which were indeed helpful. And got rid of many errors. However I have still some problematic parts. Here is modified code:
f=sym('x1^2+x2^2+x3^2+x4^2');
point=[1,1,1,1];
num=4;
k=1;
syms a
tolxy=1e-2;
n=1;
valxy=10;
x = sym('x%d', [1 num]);
%f = sum(x.^2);
%point = ones(1, num);
while(valxy>tolxy)
variable1 = [];
for i = 1:num
variable1 = [variable1 diff(f, x(i))];
end
variable2 = double(subs(variable1,x,point));
n=n+1;
valxy=norm(variable2);
d = -1*variable1;
variable3 = subs(f, x ,point + a * d);
f2=matlabFunction(variable3);
[x,y]=functx(f2,0,5) %Calling another user defined function.
k=k+1;
b=12.56;
variable4 = double(subs(variable3,[x, a],[point, b]));
point = point+b*d;
end
I wish to substitute x's by (point+a*d). Both vectors have same dimensions. Can you tell me did I do right thing? or if I want to substitute all x's by (point+a*d), what is right way to do it? f2 should only have single variable a. But now it is variable of a,x1,x2,x3,x4. By the way, again thank you for our help!
Basically what you are doing is as follow:
d = -1 * variable1;
Now let's look at it. What is variable1? It is as follows:
variable1 = [2*x1, 2*x2, 2*x3, 2*x4]
So d becomes:
d = -1 * variable1 = [-2*x1, -2*x2, -2*x3, -2*x4]
Now we compute point + a * d
point = [1, 1, 1, 1];
point + a * d = [1 - 2*a*x1, 1 - 2*a*x2, 1 - 2*a*x3, 1 - 2*a*x4]
Now when you try evaluate
variable3 = subs(f, x ,point + a * d)
then make following substitutions:
x1 --> 1 - 2*a*x1
...
...
x4 --> 1 - 2*a*x4
Due to this reason variable 3 is function of a, x1,...,x4. Now I hope you can understand the reason. Now if you have some values for x1...x4 and put them in variable3 then it will become function of just 'a'.
I hope it helps you figure out the problem. Good luck!

Sign in to comment.

Asked:

on 31 Oct 2013

Commented:

on 31 Oct 2013

Community Treasure Hunt

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

Start Hunting!