Solving linear equations with 2 unknown

Hello
I need some help to use text/string inside my function
I want to make a function that can solve 2 linear equations with 2 unknonws. At this moment my function inside my m. file looks like this (just below). But instead of (a1,b1,a2,b2) I want to write whole equations: Loesligninger('y=2x+1','y=3x-2'), and return a result like this:'x=3,y=7'
function s=LoesLigninger(a1,b1,a2,b2)
A=[-a1,1;-a2,1];
b=[b1;b2];
s=A\b;
s=inv(A)*b;
end

 Accepted Answer

You can write the equations easily enough by adding this line to the function or to your script after calling the function:
sprintf('y = %.2fx %+.2f', s)
If you want to add ‘x’ as an argument or specify it in your calling script, you can calculate and display ‘y’ as:
yf = @(s,x) s(1).*x + s(2);
sprintf('x = %.2f, y = %.2f', x, yf)
Use fprintf if you want to display to the Command Window, and sprintf if you want to save the string to output somewhere else.
Also, this line isn’t necessary:
s=inv(A)*b;

10 Comments

I tried to add the equation below the function like this:
function s=LoesLigninger2(a1,b1,a2,b2)
sprintf('y = %.2fx %+.2f',s);
yf = @(s,x) s(1).*x + s(2);
x = @(s,x) s(1).*x + s(2);
A=[-a1,1;-a2,1];
b=[b1;b2];
s=A\b;
sprintf('x = %.2f, y = %.2f', x, yf);
fprintf(s);
end
But then it saysin the command window:
>> LoesLigninger2('y=2x+1','y=3x+2')
Undefined function or variable "s".
Error in LoesLigninger2 (line 2)
sprintf('y = %.2fx %+.2f',s);
end
If I remove the 's' it says there are not enough input arguments
You have to pass ‘x’ as an argument, and rearrange the order of the statements a bit:
function s=LoesLigninger2(a1,b1,a2,b2,x)
A=[-a1,1;-a2,1];
b=[b1;b2];
s=A\b;
yf = @(s,x) s(1).*x + s(2);
sprintf('y = %.2fx %+.2f', s);
sprintf('x = %.2f, y = %.2f', x, yf);
fprintf('\n\ts(1) = % .2f, s(2) = % .2f\n', s);
end
You can use sprintf if you want to return the strings as outputs from the function (in that situation, assign them to variables in the function and then return the variables in the output), or fprintf if you want to print them directly to the Command Window.
I don’t understand what you want to do with the second construction:
LoesLigninger2('y=2x+1','y=3x+2')
There, you already have the coefficients. That is more suited to the Symbolic Math Toolbox than to core MATLAB.
Oh, the number 2 part is a written mistake, and sorry for my clumpsyness with matlab. But what should x have for an argument, when I use function the LoesLigninger(a1,b1,a2,b2,x)? When I am trying using it,it says "Undefined function or variable 'x'."
You have to include ‘x’ as an argument in the function call:
s=LoesLigninger2(a1,b1,a2,b2,x)
then the function is:
function s=LoesLigninger2(a1,b1,a2,b2,x)
A=[-a1,1;-a2,1];
b=[b1;b2];
s=A\b;
yf = @(s,x) s(1).*x + s(2);
sprintf('y = %.2fx %+.2f', s)
sprintf('x = %.2f, y = %.2f', x, yf(s,x))
fprintf('\n\ts(1) = % .2f, s(2) = % .2f\n', s)
end
There was a slight typographical error in my earlier post.
With the variables defined as:
[a1,a2,b1,b2,x] = deal(3,5,7,11,10);
the output is:
ans =
y = -2.00x +1.00
ans =
x = 10.00, y = -19.00
s(1) = -2.00, s(2) = 1.00
Thank you, Walter.
I somehow managed to mess up a simple copy-paste of working code (again). Corrected now.
I see no reason to pass in x -- not unless what you are passing in is the variable names. It appears to me that the output should be
fprintf('\n\tx = % .2f, y = % .2f\n', s)
which is to say the solution to the simultaneous equations. An external value for x does not appear to me to add anything to solving the simultaneous equations.
I agree, ‘x’ isn’t necessary for solving the equations, but I got the impression that Mathias Eriksen wanted to pass a value of ‘x’ and have the function evaluate the derived equations using it.
I could have misread the Question or later Comments. If I did, delete ‘x’ and all references to it.
Without the 'x' I have the following function:
function s=LoesLigninger1(a1,b1,a2,b2)
A=[-a1,1;-a2,1];
b=[b1;b2];
s=A\b;
yf = @(s,x) s(1).*x + s(2);
sprintf('y = %.2fx %+.2f', s)
sprintf('x = %.2f, y = %.2f', yf(s))
fprintf('\n\ts(1) = % .2f, s(2) = % .2f\n', s)
end
And when I define (a1,b1,a2,b2) before I using the function: [a1,b1,a2,b2]=(2,1,3,-2) And by then using the function I get the following written in the command window:
>> [a1,b1,a2,b2]=deal(2,1,3,-2);
>> LoesLigninger2(a1,b1,a2,b2)
ans =
y = 3.00x +7.00
Error using LoesLigninger2>@(s,x)s(1).*x+s(2)
(line 5)
Not enough input arguments.
Error in LoesLigninger2 (line 7)
sprintf('x = %.2f, y = %.2f', yf(s))
Where it says y=3.00+7.00 is close to the right answer. It should have written that x=3 and y=7
What have I done wront till now?
function s=LoesLigninger1(a1,b1,a2,b2)
A=[-a1,1;-a2,1];
b=[b1;b2];
s=A\b;
fprintf('\n\tx = % .2f, y = % .2f\n', s);
Thank you both, with yours help it works now. The part where I need to write the function LoesLigninger('y=2x+1','y=3x-2') is not important as the return of x=3,y=7 (thumps up)

Sign in to comment.

More Answers (1)

In order to be able to pass the equations in as strings, you need to define the permitted syntaxes. For example do you need to be able to call,
LoesLigninger2('y is ummm, three g plus pi','let y be, I don''t know, say five more than twice the other variable maybe? What about that?')
Even if you restrict yourself to 'x' and 'y', you need to define whether multiplication by 1 needs to written in, and you need to define whether addition or subtraction of 0 needs to be written in, and you need to know about allowed spacing and about whether tabs are allowed. Is 'y = x' to be allowed or would it have to be written as 'y=1x+0' ? Are fractions to be allowed? Is floating point to be allowed? Is the imaginary unit sqrt(-1) to be represented by "i" or by "I" or by "j" ? When the constant multiples are being formed is it allowed to specify them as (for example) "sqrt(2)" ? Are negative multiples allowed? Can the user use brackets? Can the user use "unary plus" and "unary minus" such as in 'y=+3x+-7' ?
The more flexibility you allow, the harder it is to parse. If you restrict the flexibility a lot it might be easy to process.

Community Treasure Hunt

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

Start Hunting!