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:
finding zero crossing of plot/function

Subject: finding zero crossing of plot/function

From: mistersink sink

Date: 10 May, 2010 14:31:05

Message: 1 of 7

Hi everyone,
I'm pretty new to Matlab and I need some help.
here is an excerpt of my code:
-----
clear
clc
Vl=12.092904976150820;
e=[0.058277693076074 0.085256559560097 0.128585397653194];
Ve=[2.599650000000001 4.159440000000000 4.159440000000000];
variable=-5:.0005:5;
maxsize=(size(variable));
for i=1:maxsize(1,2)
summ2(i)=Ve(1)*log(e(1)-e(1)/(1-variable(i)*Ve(1)))+Ve(2)*log(e(2)-e(2)/(1-variable(i)*Ve(2)))+Ve(3)*log(e(3)-e(3)/(1-variable(i)*Ve(3)));
end
constraint=real(summ2)+Vl;
plot(variable,constraint)
-----
This basically plots a complicated function of "variable" over a range from -5 to 5.
It creates a plot with two crossings of the x-axis. I've manually found these to be approximately variable=.2020 and variable=.4144.
the value .2020 is no good and i want to find the exact value of the .4144 root.
I will be using this value later on, so is there a way to find this using code?
Also, it would be nice if i could mark these two constraint=0 crossings on the plot. It would be nice if I could put it in the code, but doing it with plot tools would be ok too.

Thanks for your help!
-mistersink

Subject: finding zero crossing of plot/function

From: Sean

Date: 10 May, 2010 14:51:26

Message: 2 of 7

"mistersink sink" <zpsink@gmail.com> wrote in message <hs95b9$spu$1@fred.mathworks.com>...
> Hi everyone,
> I'm pretty new to Matlab and I need some help.
> here is an excerpt of my code:
> -----
> clear
> clc
> Vl=12.092904976150820;
> e=[0.058277693076074 0.085256559560097 0.128585397653194];
> Ve=[2.599650000000001 4.159440000000000 4.159440000000000];
> variable=-5:.0005:5;
> maxsize=(size(variable));
> for i=1:maxsize(1,2)
> summ2(i)=Ve(1)*log(e(1)-e(1)/(1-variable(i)*Ve(1)))+Ve(2)*log(e(2)-e(2)/(1-variable(i)*Ve(2)))+Ve(3)*log(e(3)-e(3)/(1-variable(i)*Ve(3)));
> end
> constraint=real(summ2)+Vl;
> plot(variable,constraint)
> -----
> This basically plots a complicated function of "variable" over a range from -5 to 5.
> It creates a plot with two crossings of the x-axis. I've manually found these to be approximately variable=.2020 and variable=.4144.
> the value .2020 is no good and i want to find the exact value of the .4144 root.
> I will be using this value later on, so is there a way to find this using code?
> Also, it would be nice if i could mark these two constraint=0 crossings on the plot. It would be nice if I could put it in the code, but doing it with plot tools would be ok too.
>
> Thanks for your help!
> -mistersink

>>help fzero
Create a function file with your function, pass it as a function handle and make an initial guess 0.4144 or 0.2020.

Subject: finding zero crossing of plot/function

From: mistersink sink

Date: 10 May, 2010 15:17:09

Message: 3 of 7


> Create a function file with your function, pass it as a function handle and make an initial guess 0.4144 or 0.2020.

I'm sorry, I don't understand. how do you create a function file and pass it as a function handle?

Subject: finding zero crossing of plot/function

From: Sean

Date: 10 May, 2010 15:36:21

Message: 4 of 7

"mistersink sink" <zpsink@gmail.com> wrote in message <hs981l$s0r$1@fred.mathworks.com>...
>
> > Create a function file with your function, pass it as a function handle and make an initial guess 0.4144 or 0.2020.
>
> I'm sorry, I don't understand. how do you create a function file and pass it as a function handle?

Here:
%%%%%new .m file
<top line of file> function [y] = myfunction(x);
%x is your variable
%y is the value

Vl=12.092904976150820;
e=[0.058277693076074 0.085256559560097 0.128585397653194];
Ve=[2.599650000000001 4.159440000000000 4.159440000000000];
summ2=Ve(1)*log(e(1)-e(1)/(1-x*Ve(1)))+Ve(2)*log(e(2)-e(2)/(1-x*Ve(2)))+Ve(3)*log(e(3)-e(3)/(1-x*Ve(3)));

y=real(summ2)+Vl;
% I think I changed it correctly though no guarantee I ran it and it worked
%%%%%%%%

>>fzero(@myfunction, .4)
ans = 0.414394760826850
>>fzero(@myfunction, .1)
ans = 0.202001240136841


Make sure to use:
>> format long
to visualize it so you can see the full floating point value.

Subject: finding zero crossing of plot/function

From: mistersink sink

Date: 10 May, 2010 18:17:29

Message: 5 of 7

"Sean " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <hs995l$d7j$1@fred.mathworks.com>...
> "mistersink sink" <zpsink@gmail.com> wrote in message <hs981l$s0r$1@fred.mathworks.com>...
> >
> > > Create a function file with your function, pass it as a function handle and make an initial guess 0.4144 or 0.2020.
> >
> > I'm sorry, I don't understand. how do you create a function file and pass it as a function handle?
>
> Here:
> %%%%%new .m file
> <top line of file> function [y] = myfunction(x);
> %x is your variable
> %y is the value
>
> Vl=12.092904976150820;
> e=[0.058277693076074 0.085256559560097 0.128585397653194];
> Ve=[2.599650000000001 4.159440000000000 4.159440000000000];
> summ2=Ve(1)*log(e(1)-e(1)/(1-x*Ve(1)))+Ve(2)*log(e(2)-e(2)/(1-x*Ve(2)))+Ve(3)*log(e(3)-e(3)/(1-x*Ve(3)));
>
> y=real(summ2)+Vl;
> % I think I changed it correctly though no guarantee I ran it and it worked
> %%%%%%%%
>
> >>fzero(@myfunction, .4)
> ans = 0.414394760826850
> >>fzero(@myfunction, .1)
> ans = 0.202001240136841
>
>
> Make sure to use:
> >> format long
> to visualize it so you can see the full floating point value.

Thanks for the reply.
I tried this, but it told me that "??? Input argument "x" is undefined." for the summ2 line.
also do i make this in another .m file and run it first? i'm not too sure about how functions work and calling them. when i placed the code in the long version of my code, it said "Function definitions are not permitted in this context."

Subject: finding zero crossing of plot/function

From: Sean

Date: 10 May, 2010 18:40:22

Message: 6 of 7

"mistersink sink" <zpsink@gmail.com> wrote in message <hs9ijp$6mc$1@fred.mathworks.com>...
> "Sean " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <hs995l$d7j$1@fred.mathworks.com>...
> > "mistersink sink" <zpsink@gmail.com> wrote in message <hs981l$s0r$1@fred.mathworks.com>...
> > >
> > > > Create a function file with your function, pass it as a function handle and make an initial guess 0.4144 or 0.2020.
> > >
> > > I'm sorry, I don't understand. how do you create a function file and pass it as a function handle?
> >
> > Here:
> > %%%%%new .m file
> > <top line of file> function [y] = myfunction(x);
> > %x is your variable
> > %y is the value
> >
> > Vl=12.092904976150820;
> > e=[0.058277693076074 0.085256559560097 0.128585397653194];
> > Ve=[2.599650000000001 4.159440000000000 4.159440000000000];
> > summ2=Ve(1)*log(e(1)-e(1)/(1-x*Ve(1)))+Ve(2)*log(e(2)-e(2)/(1-x*Ve(2)))+Ve(3)*log(e(3)-e(3)/(1-x*Ve(3)));
> >
> > y=real(summ2)+Vl;
> > % I think I changed it correctly though no guarantee I ran it and it worked
> > %%%%%%%%
> >
> > >>fzero(@myfunction, .4)
> > ans = 0.414394760826850
> > >>fzero(@myfunction, .1)
> > ans = 0.202001240136841
> >
> >
> > Make sure to use:
> > >> format long
> > to visualize it so you can see the full floating point value.
>
> Thanks for the reply.
> I tried this, but it told me that "??? Input argument "x" is undefined." for the summ2 line.
> also do i make this in another .m file and run it first? i'm not too sure about how functions work and calling them. when i placed the code in the long version of my code, it said "Function definitions are not permitted in this context."


Of course it did, you have to give the function a variable in order for it to work.

You need to:
1) Open a standalone mfile and copy in the function just I showed above.
the top line should be function... and 'function' should be blue otherwise there's an error.
2) In order to run this function you need to do something such as this
>>myfunction(3) % Your result function with variable=3;
Then from somewhere else; your other script, or command window, you can call fzero as I did above.
If you just say >>myfunction; it's not receiving an 'x' and thus throwing the error.
You also can't have functions in scripts, the other error you're seeing.

For a better explanation and examples with functions:
>> doc function
>> doc function_handle

Subject: finding zero crossing of plot/function

From: mistersink sink

Date: 10 May, 2010 23:21:07

Message: 7 of 7

"Sean " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <hs9jum$4qd$1@fred.mathworks.com>...
> "mistersink sink" <zpsink@gmail.com> wrote in message <hs9ijp$6mc$1@fred.mathworks.com>...
> > "Sean " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <hs995l$d7j$1@fred.mathworks.com>...
> > > "mistersink sink" <zpsink@gmail.com> wrote in message <hs981l$s0r$1@fred.mathworks.com>...
> > > >
> > > > > Create a function file with your function, pass it as a function handle and make an initial guess 0.4144 or 0.2020.
> > > >
> > > > I'm sorry, I don't understand. how do you create a function file and pass it as a function handle?
> > >
> > > Here:
> > > %%%%%new .m file
> > > <top line of file> function [y] = myfunction(x);
> > > %x is your variable
> > > %y is the value
> > >
> > > Vl=12.092904976150820;
> > > e=[0.058277693076074 0.085256559560097 0.128585397653194];
> > > Ve=[2.599650000000001 4.159440000000000 4.159440000000000];
> > > summ2=Ve(1)*log(e(1)-e(1)/(1-x*Ve(1)))+Ve(2)*log(e(2)-e(2)/(1-x*Ve(2)))+Ve(3)*log(e(3)-e(3)/(1-x*Ve(3)));
> > >
> > > y=real(summ2)+Vl;
> > > % I think I changed it correctly though no guarantee I ran it and it worked
> > > %%%%%%%%
> > >
> > > >>fzero(@myfunction, .4)
> > > ans = 0.414394760826850
> > > >>fzero(@myfunction, .1)
> > > ans = 0.202001240136841
> > >
> > >
> > > Make sure to use:
> > > >> format long
> > > to visualize it so you can see the full floating point value.
> >
> > Thanks for the reply.
> > I tried this, but it told me that "??? Input argument "x" is undefined." for the summ2 line.
> > also do i make this in another .m file and run it first? i'm not too sure about how functions work and calling them. when i placed the code in the long version of my code, it said "Function definitions are not permitted in this context."
>
>
> Of course it did, you have to give the function a variable in order for it to work.
>
> You need to:
> 1) Open a standalone mfile and copy in the function just I showed above.
> the top line should be function... and 'function' should be blue otherwise there's an error.
> 2) In order to run this function you need to do something such as this
> >>myfunction(3) % Your result function with variable=3;
> Then from somewhere else; your other script, or command window, you can call fzero as I did above.
> If you just say >>myfunction; it's not receiving an 'x' and thus throwing the error.
> You also can't have functions in scripts, the other error you're seeing.
>
> For a better explanation and examples with functions:
> >> doc function
> >> doc function_handle

thanks that worked. now is there any way to point the cursor on a plot to a certain value of x or y? i can get it close if i drag it, but i want to be more precise.

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