Home > Blockchain >  How do I solve for X on Matlab when X is on both sides of the equation
How do I solve for X on Matlab when X is on both sides of the equation

Time:01-31

Suppose i have an equation that looks like this: x = y zx

I have the values for y and z, I want to calculate x. How would I do this on Matlab without having to solve for x on paper?

Probably a simple equation but I am new, thank you!

I tried just writing the equation out and coding disp(x), but of course it did not work.

CodePudding user response:

The function fzero() will give you a numeric answer if you move all of one side of the equation over to the other side, so the first side is equal to zero.

y = 4;
z = 2;
% Move everything to the right side
zero_left_side = @(x)(y z*x) - (x);

initial_guess = 1;
x_solution = fzero(zero_left_side, initial_guess);

CodePudding user response:

You can use the solve function in MATLAB to solve for an unknown variable in an equation. To solve for X in an equation like "X = F(X)" you need to use a numerical solution method such as "solve". For example:

syms X F = @(X) X^2 - 4; X = fsolve(F,2)

This will find the numerical solution of the equation X^2-4=0 starting from an initial guess of X=2.

  • Related