MATLAB Answers

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

조회 수: 2(최근 30일)
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;
CC= 0.0283;
LEVEL = 26.5155;
PRAM = 70.7090;
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:
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)

  댓글 수: 0

로그인 to comment.

채택된 답변

Are Mjaavatten
Are Mjaavatten 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;
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);

  댓글 수: 1

Luigi Pisano
Luigi Pisano 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

로그인 to comment.

More Answers (0)

이 질문에 답변하려면 로그인을(를) 수행하십시오.


Translated by