help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: solve simultaneous equation (novice)


From: Martijn Brouwer
Subject: Re: solve simultaneous equation (novice)
Date: Wed, 28 May 2014 11:34:01 +0200

Apart from the fact that you do not need fsolve for linear equations,
the implementation of your function is wrong.\
You want to fined the find the roots of a system of two equations, with
two input variables, x and y. fsolve needs an function of one variable,
returning one variable. Both can be an arrays. Moreover, your function
does not return anything because you do not assign a value to y. So
rephrase your function:

function y = f(x)
x1=x(1);
x2=x(2);
y1=4.2-2*x1-4*x2;
y2=6.3-4*x1-x2;
y = [y1;y2];
end

[solution, err, info]=fsolve(@f, [1;2])
solution =

   1.50000
   0.30000

err =

  -9.1673e-12
  -2.2204e-16

info =  1

Of course this definition is a little convoluted. I would use 

function y = f(x)
y1=4.2-2*x(1)-4*x(2);
y2=6.3-4*x(1)-x(2);
y = [y1;y2];
end

This is how to solve non-linear equations, for linear equations such as
yours use the \ operator.

Good luck,

Martijn


On Sun, 2014-05-25 at 19:05 +0000, message wrote:
> Readers,
> 
> The function 'fsolve' was attempted to be used to solve the following 
> simultaneous equations:
> 
> equation 1: 4.2=2x+4y; equation 2: 6.3=4x+y
> 
> function y=f(x)
> ya=-4.2+2*x+4*4.2/4-2*x/4;
> yb=-6.3+4*x+4.2/4-2*x/4;
> endfunction
> [x,info]=fsolve(@f,[1;2])
> 
> warning: f: some elements in list of return values are undefined
> warning: f: some elements in list of return values are undefined
> warning: f: some elements in list of return values are undefined
> warning: f: some elements in list of return values are undefined
> warning: f: some elements in list of return values are undefined
> warning: f: some elements in list of return values are undefined
> x =
> 
>     1
>     2
> 
> info = [](0x0)
> 
> Manual solution:
> 
> Rearrangement to
> 4y=4.2-2x, 4.2/4-2x/4
> 
> 6.3=4x+4.2/4-2x/4 -> 25.2=16x+4.2-2x -> 25.2-4.2=16x-2x -> 21=14x -> 
> x=3/2
> 
> 4.2=2*3/2+4y -> 4.2=3+4y -> 1.2=4y -> 1.2/4=y -> y=.3
> 
> Please what is my error in octave syntax to prevent the same result in 
> octave as in the manual method described?
> 
> _______________________________________________
> Help-octave mailing list
> address@hidden
> https://lists.gnu.org/mailman/listinfo/help-octave





reply via email to

[Prev in Thread] Current Thread [Next in Thread]