How to declare variables equal to the number of variables entered by the operator
2 views (last 30 days)
Show older comments
I am developing a code for Newton's method for non-linear equations. I intend to take an input of no. of eq's from the user and declare variables equal to the number of equations e.g. No. of eq = 3 then syms x1,x2,x3 or x,y,z. Also, I can't store the equations in a column vector and take the jacobian. I also am unable to evaluate the functions and jacobian at a set of coordinates.I have developed a starting for 2 eq's.
syms x y clear f1 f2
f1=input('Enter First eq:','s');
f2=input('Enter second eq:','s');
X=[0 0];
f1=4*x^2-20*x+1/4*(y^2)+8;
f2=1/2*(x*y^2)+2*x-5*y+8;
j1=jacobian([f1],[x y]);
j2=jacobian([f2],[x y]);
f1=inline(f1,'x','y');
f2=inline(f2,'x','y');
c1=f1(X(1),X(2));
c2=f2(X(1),X(2));
F=[c1 c2]'
j1=inline(j1,'x','y');
j2=inline(j2,'x','y');
c=j1(X(1),X(2));
d=j2(X(1),X(2));
J=[c;d]
Please help me to modify this code for any number of equations entered by the user.
0 Comments
Accepted Answer
Matt J
on 6 Nov 2014
Edited: Matt J
on 6 Nov 2014
I intend to take an input of no. of eq's from the user and declare variables equal to the number of equations
No, don't do this. Stick with the approach we've been discussing in your similar post
Instead of having separate f1,f2, etc... develop a single vector-valued symbolic function f. Then you can apply jacobian, gradient, etc... to f with matrix/vector semantics.
0 Comments
More Answers (0)
See Also
Categories
Find more on Function Creation in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!