matlab linear equations with mod

628 views Asked by At

I have a simple equations to solve and I want to do it in matlab,however, there are some years, that I didn't use and kinda forgot it. So this

linsolve({(387=mod(324*a+b,601)), (491=mod(381*a+b,601))},{a,b}),

doesn't give me a result, due to an a mistake I am not able to find. the original set of equations is:

(324a+b)mod601=387 (381a+b)mod601=491,

affine cipher apparently. Thank you!

2

There are 2 answers

0
David K On

Starting with these equations,

(324*a + b) mod 601 = 387,
(381*a + b) mod 601 = 491,

we can conclude that

((381*a + b) - (324*a + b)) mod 601 = (491 - 387) mod 601

and therefore (performing the obvious simplifications inside the parentheses on the left-hand side and fully evaluating the right-hand side),

(57*a) mod 601 = 104.

That looks a lot easier to solve than the system of two equations in two variables. Once you know a, you can return to one of the original equations and solve for b.

4
freude On

Try this First make you function describing equations in a separate file with the name myfunc.m:

function y=myfunc(x)
    y(1)=mod(324*x(1)+x(2),601)-387;
    y(2)=mod(381*x(1)+x(2),601)-491;

than apply fsolve like

x0=[1; 1];
[ans,err]=fsolve(@myfunc,x0)

x0 is a guess to the solution. This method gives an approximate numerical solution around your guess.

fsolve solves a system of nonlinear equations. It has many parameters, you can read about them in MATLAB help.

Let me know if it works