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

조회 수: 1 (최근 30일)
Luigi Pisano
Luigi Pisano 2018년 11월 25일
댓글: Luigi Pisano 2018년 11월 26일
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)

채택된 답변

Are Mjaavatten
Are Mjaavatten 2018년 11월 26일
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
Luigi Pisano
Luigi Pisano 2018년 11월 26일
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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graph and Network Algorithms에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by