Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
super simple code question

Subject: super simple code question

From: Aram

Date: 15 Feb, 2009 04:30:05

Message: 1 of 8

Hello guys,

I just start using matlab and have a quick question.

Say I have a two functions y1=sin(x) and y2=0.5.

To find the x value that two graphs intersections, I want to build a code using "for" sentence. say for 1:length(x) y1-y2<0.0000....

Hope someone can make the code for me.

Thank you,
AJ
                          
001 ,

Subject: super simple code question

From: Occa O'Connor

Date: 15 Feb, 2009 05:28:02

Message: 2 of 8

G'DAy Aram,

A smart bloke called Matt Fig made a function which solves non-linear equations very easily. To download and read go to:

http://www.mathworks.com.au/matlabcentral/fileexchange/6924

Then use it like this:

y1 = @(x)sin(x)
y2 = 0.5
y_zero = @(x)y1(x) - y2
newtzero(y_zero, 1)

Cheers

Occa




"Aram " <ac467@cornell.edu> wrote in message <gn85oc$qqv$1@fred.mathworks.com>...
> Hello guys,
>
> I just start using matlab and have a quick question.
>
> Say I have a two functions y1=sin(x) and y2=0.5.
>
> To find the x value that two graphs intersections, I want to build a code using "for" sentence. say for 1:length(x) y1-y2<0.0000....
>
> Hope someone can make the code for me.
>
> Thank you,
> AJ
>
> 001 ,

Subject: super simple code question

From: Nasser Abbasi

Date: 15 Feb, 2009 05:43:46

Message: 3 of 8


"Aram " <ac467@cornell.edu> wrote in message
news:gn85oc$qqv$1@fred.mathworks.com...
> Hello guys,
>
> I just start using matlab and have a quick question.
>
> Say I have a two functions y1=sin(x) and y2=0.5.
>
> To find the x value that two graphs intersections, I want to build a code
> using "for" sentence. say for 1:length(x) y1-y2<0.0000....
>
> Hope someone can make the code for me.
>
> Thank you,
> AJ
>
> 001 ,


EDU>> syms x
EDU>> solve(sin(x)-1/2,x)

ans =
     pi/6
 (5*pi)/6

So, just add multiplies of 2 pi to each of the above solutions to get all
other solutions

--Nasser

Subject: super simple code question

From: AJ

Date: 15 Feb, 2009 06:09:01

Message: 4 of 8

"Nasser Abbasi" <nma@12000.org> wrote in message <qYNll.17818$c45.4491@nlpi065.nbdc.sbc.com>...
>
> "Aram " <ac467@cornell.edu> wrote in message
> news:gn85oc$qqv$1@fred.mathworks.com...
> > Hello guys,
> >
> > I just start using matlab and have a quick question.
> >
> > Say I have a two functions y1=sin(x) and y2=0.5.
> >
> > To find the x value that two graphs intersections, I want to build a code
> > using "for" sentence. say for 1:length(x) y1-y2<0.0000....
> >
> > Hope someone can make the code for me.
> >
> > Thank you,
> > AJ
> >
> > 001 ,
>
>
> EDU>> syms x
> EDU>> solve(sin(x)-1/2,x)
>
> ans =
> pi/6
> (5*pi)/6
>
> So, just add multiplies of 2 pi to each of the above solutions to get all
> other solutions
>
> --Nasser
>
>

First thank you very much for your tips but I want to run loop to find out the intersections. Any suggestions?

Best,
AJ

Subject: super simple code question

From: Matt Fig

Date: 15 Feb, 2009 06:54:01

Message: 5 of 8

As others have mentioned, there are better ways to do this. You could use a rootfinder (Matlab's fzero or another of your choice) on the compound equation, or solve symbolically (even without the symbolic toolbox it is easy enough for your equation).

But you *could* do this in a for loop. Since I have a feeling this is a homework problem, I will give you a bare-bones starter code with no comments, that still needs a lot of work and refinement. This is just off the top of my head, so there may even be some mistakes, but it does find a root of your equation, as is shown in the example. Take this for just that, you can make this much better!

%-------------------------------------------------------------------------------------%
function [rt] = newroot(f,start,finish)
% NEWROOT(f,start,finish) find first root of f between start and finsih.
% Make sure start is left of the root and finish to the right.
% Example:
% f = @(x)sin(x)-.5;
% rtnewroot = newroot(f,0,1);
% rt = fzero(f,.5) % Use Matlab's built in as a check.
% rtnewroot-rt

tol = 1e-15;
rt = start;
inc = .1;
tol_loop = 1.2*inc;

while abs(f(rt))>tol
    for ii = start:inc:finish
        if abs(f(ii))<tol_loop
            rt = ii;
            start = ii;
            inc = inc/10;
            tol_loop = 1.2*inc;
            break
        end
    end
end
%-------------------------------------------------------------------------------------%




vrp6t$t//~6qI/r%/p|O pw= ~~x{}qtxh/\|w&$~tz%)}{pTp~p~$/wp//

Subject: super simple code question

From: AJ

Date: 16 Feb, 2009 03:54:02

Message: 6 of 8

"Matt Fig" <spamanon@yahoo.com> wrote in message <gn8e69$m1v$1@fred.mathworks.com>...
> As others have mentioned, there are better ways to do this. You could use a rootfinder (Matlab's fzero or another of your choice) on the compound equation, or solve symbolically (even without the symbolic toolbox it is easy enough for your equation).
>
> But you *could* do this in a for loop. Since I have a feeling this is a homework problem, I will give you a bare-bones starter code with no comments, that still needs a lot of work and refinement. This is just off the top of my head, so there may even be some mistakes, but it does find a root of your equation, as is shown in the example. Take this for just that, you can make this much better!
>
> %-------------------------------------------------------------------------------------%
> function [rt] = newroot(f,start,finish)
> % NEWROOT(f,start,finish) find first root of f between start and finsih.
> % Make sure start is left of the root and finish to the right.
> % Example:
> % f = @(x)sin(x)-.5;
> % rtnewroot = newroot(f,0,1);
> % rt = fzero(f,.5) % Use Matlab's built in as a check.
> % rtnewroot-rt
>
> tol = 1e-15;
> rt = start;
> inc = .1;
> tol_loop = 1.2*inc;
>
> while abs(f(rt))>tol
> for ii = start:inc:finish
> if abs(f(ii))<tol_loop
> rt = ii;
> start = ii;
> inc = inc/10;
> tol_loop = 1.2*inc;
> break
> end
> end
> end
> %-------------------------------------------------------------------------------------%
>
>
>
>
> vrp6t$t//~6qI/r%/p|O pw= ~~x{}qtxh/\|w&$~tz%)}{pTp~p~$/wp//

Hello Matt,

Thank you for your help. I've tried to understand your skeleton code but still don't get "rt=start and tol_loop = 1.2*inc" part. Any advice on these?

Thank you!
-Aram

p.s. FYI, this is not a homework. I just don't want to solve this using root finder (say fzero) and just want to practice loop. :)

Subject: super simple code question

From: Matt Fig

Date: 16 Feb, 2009 04:42:02

Message: 7 of 8

"AJ " <ac467@cornell.edu> wrote in message
> Thank you for your help. I've tried to understand your skeleton code but still don't get "rt=start and tol_loop = 1.2*inc" part. Any advice on these?
>
> Thank you!
> -Aram
>
> p.s. FYI, this is not a homework. I just don't want to solve this using root finder (say fzero) and just want to practice loop. :)


The basic idea was to follow your line of thinking (as partially outlined in your first post). That is, assume the root is the left bracket, and march towards the right bracket in successively smaller increments as we approach the root. Every time a value for root estimate is small enough (less than some arbitrary number, tol_loop), start looking in smaller increments.
As I mentioned, I wrote this out in about 3 minutes as an outline to get you started. What I was trying to do with 1.2*inc, is make sure that we don't march past the actual root before decreasing the increment.
It looked to me, after reading your first post, that this sort of thing is what you had in mind...
If you are just doing this for an exercise, that is fine. Good luck.




k,F <F</f6(M65=6`0;4*/7(FM2/FF((6F@;FF4(;,T*.)65,3s)3((7,06

Subject: super simple code question

From: Occa O'Connor

Date: 16 Feb, 2009 05:24:02

Message: 8 of 8

G'Day Aram

Finding the solutions to two equations numerically, particularily when non-linear is not a trivial thing. There are many methods or which all would iterate in a for loop, bi-section, newton etc... To have an overview of these alogrithum read these, and follow the links.

http://en.wikipedia.org/wiki/Root-finding_algorithm

Cheers

Occa

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us