saving real roots in a vector; "solve" gives me weird results

2 views (last 30 days)
Hi - I want to save in a vector the coordinates of the points in which one function reaches a certain level.
For reference, let's consider the following function and graph:
clear all; close all; clc;
GAMMA =1.1;
AHIGH=1;
ALOW=0.4;
CC= 0.0283;
LEVEL = 26.5155;
PRAM = 70.7090;
% PLOT GRAPH
f = @(x,y) ALOW*x*GAMMA*PRAM/sqrt(x^2 +1)-ALOW*(x-CC)/AHIGH-CC-y;
fimplicit(f,[-2 100 -50 50])
hold on
xplot1 = -2:0.01:100;
lineplot33= LEVEL*ones(1, length(xplot1));
plot(xplot1, lineplot33)
hold off
One can see from the graph that the function f reaches the level LEVEL = 26.5155 in two points; by eye-balling them, their coordinates on the X axis should be around 3 and 13, respectively.
I want to save those 2 values in a vector. I'm trying to do that using "solve" but I get weird results. (I'm not very proficient with the Symbolic toolbox I'm afraid).
What's a better way to do that? Here's my current (wrong) code:
% I WANT TO SAVE THE "ROOTS" IN A VECTOR
syms x real
eqn2=LEVEL==ALOW*x*GAMMA*PRAM/sqrt(x^2 +1)-ALOW*(x-CC)/AHIGH-CC;
[SOLVE21, prams, cnds]= solve(eqn2,x,'ReturnConditions',true)

Accepted Answer

Are Mjaavatten
Are Mjaavatten on 26 Nov 2018
The code below will solve your problem as stated. In the general case with other parameters it may not be robust. You may need to specify other starting points (or intervals) for fzero. And, of course, a solution may not always exist.
Note the dots before operators / and ^ operators in the expression for g. Those make it able to handle vector x values.
GAMMA =1.1;
AHIGH=1;
ALOW=0.4;
CC= 0.0283;
LEVEL = 26.5155;
PRAM = 70.7090;
g = @(x) ALOW*x*GAMMA*PRAM./sqrt(x.^2 +1)-ALOW*(x-CC)/AHIGH-CC-LEVEL;
xsol = zeros(2,1);
xsol(1) = fzero(g,-2);
xsol(2) = fzero(g,100);
xplot = linspace(-2,100);
plot(xplot,g(xplot),xsol,g(xsol),'*r')
  1 Comment
Luigi Pisano
Luigi Pisano on 26 Nov 2018
OK thanks. I see what you're doing and it works fine in my example. I will need to use it for several functions so I might need to tinker with it a bit but it's an excellent starting point

Sign in to comment.

More Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!