Is there a function I can use to find out when a statement is true?

Hello,
I'm new to matlab.
Is there a function I can use to find out when a statement is true?
Simple example: for which "x" the equation is true.
1+x = 3
Can somebody help me?
Alex

Answers (2)

in your case, if ... else ... end structure should work very well.
See this. It uses symbolic toolbox to solve the equations.
syms x
eq = 1+x == 3;
sol = solve(eq);
Result:
>> sol
sol =
2
See documentation of solve() function for more examples.

3 Comments

Great. Thanks.
I tested a few functions with your tips and they worked well.
However, this one doesn't work:
1/(10^x(10^x+1)).
any ideas?
Hi Alexander
Can you please share the error message when you solve this equation. Also, in order to solve 1/(10^x * (10^x+1)), represent this in form of an equation ,eg. 1/(10^x*(10^x+1)) == "some value". Then it would work. Like i tried the following
>> syms z
>> eq = 1/(10^z * (10^z + 1)) == 10
eq =
1/(10^z*(10^z + 1)) == 10
>> sol = solve(eq)
sol =
log(- 35^(1/2)/10 - 1/2)/log(10)
log(35^(1/2)/10 - 1/2)/log(10)
I got the same answer as Karan. But if you are using release older than R2020a, than it is possible that MATLAB didn't return a solution. It seems that MATLAB has made some improvements to the symbolic engine in the new release, and it is able to solve equations that were not possible before. In that case, you will need to rely on a numerical solution
eq = @(x) 1/(10^x*(10^x+1)) - 10;
sol = fsolve(eq, rand);
The downside is that it can only give one solution at a time.

This question is closed.

Asked:

on 23 Apr 2020

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!