iam trying to input two equations throw edit text in matlab gui and i want to know what is the error (iam solving by newton's method for non-linear equations)

1 view (last 30 days)
syms x y
f1=get(handles.f1,'string');
fn1=inline(f1);
f2=get(handles.f2,'string');
fn2=inline(f2);
g1=get(handles.g1,'string');
g2=get(handles.g2,'string');
n=get(handles.n,'string');
g1=str2double(g1);
g2=str2double(g2);
n=str2double(n);
xo =[g1;g2];
h=[fn1;fn2];
g=[diff(fn1,x) diff(fn1,y);diff(fn2,x) diff(fn2,y)];
j=inv(g);
for i=1:n
f= double(subs(h,{x,y},{[xo(1)],[xo(2)]}));
j= double(subs(g,{x,y},{[xo(1)],[xo(2)]}));
xn=xo-j*f;
xo=xn;
out=xn;
end
out=num2str(out);
set(handles.out,'string',out)
  4 Comments
Geoff Hayes
Geoff Hayes on 16 May 2015
Please clarify what you mean by he said Matrix dimensions must agree. Which line of your code does this error message correspond to? Typically it means that you are trying to do some sort of operation (addition, subtraction, multiplication, etc.) between two matrices that have incompatible dimensions for the operation..
abdelaziz abdelaziz
abdelaziz abdelaziz on 16 May 2015
the error says : inline function cant be concatenated and also there is error
h=[fn1;fn2];
i dont now what is meant by that errors

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 16 May 2015
You need to use double() on the symbolic numbers, such as
f = double(subs(h,{x,y},{[xo(1)],[xo(2)]}));
  1 Comment
Walter Roberson
Walter Roberson on 16 May 2015
Do not use inline functions. For example, recode
fn1=inline(f1);
to
fn1 = matlabFunction(sym(f1));
or
fn1t = str2func(['@(x) ' fn]); %generate anonymous function
fn1 = fn1t(x); %apply it at the symbol x
Which one you use will depend upon whether the expression provided by the user is going to be in symbolic toolbox notation or in MATLAB notation. For example for x+x^2+x^3 can the user write
sum(x^n,n=1..3)
which is symbolic notation, or would they write
sum(x.^(1:n))
which is MATLAB notation.

Sign in to comment.

Categories

Find more on Symbolic Math Toolbox in Help Center and File Exchange

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!