How to Solve an equation using if statements?

2 views (last 30 days)
I am trying to solve this equation. I know I have to write if statements but I forgot how to do it. The equation is : a^4+b^2+c^2=2014 and a+b+c=?.
I know I have to set each variable up an interval.
Thank you.
  3 Comments
John D'Errico
John D'Errico on 11 Jul 2015
Edited: John D'Errico on 11 Jul 2015
WAY more work than is necessary. For example, do you really need to look as far as 100 for a? What is 100^4? Is it considerably larger than 2014?
So is there any reason to look further out for a than floor(nthroot(2014,4))==6?
Likewise, how far out did you need to look at b and c?
Robert
Robert on 18 Jun 2016
I wasn't focused on optimizing initially when I wrote it. However, I can see your point. 100 is way too high to start.

Sign in to comment.

Accepted Answer

John D'Errico
John D'Errico on 18 Jun 2016
Edited: John D'Errico on 18 Jun 2016
Has nobody ever answered this? So
sqrt(2014)
ans =
44.878
nthroot(2014,4)
ans =
6.6991
A needs never go past 6. b needs never go past 44. In fact, since b and c are symmetrical, assuming you wish to find unique solutions, just assume that b>=c. Therefore, you can limit c to vary from 0 to b. Yes, this does find all unique solutions, since if you had a solution where b was less than c, then just swap them, and you have another solution, with b>=c.
So the above scheme would work using loops.
A simple non-looping solution might look like this:
target = 2014;
amax = floor(nthroot(target,4));
bmax = sqrt(target);
cmax = bmax;
[aa,bb,cc] = ndgrid(0:amax,0:bmax,0:cmax);
ind = find((aa.^4 + bb.^2 + cc.^2) == target);
[aa(ind),bb(ind),cc(ind)]
ans =
3 42 13
3 13 42
As you can see, there are two solutions, symmetrical in b and c. As well, the ndgrid solution is the way one would normally solve it in MATLAB, since it does not force you to set up lots of loops, tests, breaks,etc.
  1 Comment
Robert
Robert on 18 Jun 2016
I like the way to not have to use multiple loops. It seems like the more code you have the more change of error can happen.
Which is why I keep having problems with it now? Maybe you can help me out now? See link below.
I have found that isn't running through the loops more than once. Then I can't figure out how to assign T(1) has a not a set number.

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!