Does anyone know how to get solve to work with one of the variables being a range

1 回表示 (過去 30 日間)
Lauren
Lauren 2024 年 12 月 12 日
編集済み: John D'Errico 2024 年 12 月 12 日
clc
clear
syms x i ii iii
x= linspace(-5,5, 11);
i = solve (6*x-5 == 97);
Incorrect number or types of inputs or outputs for function solve.
ia =double(i)
ii = solve (x^2-7*x+3 == 0)
iia =double(ii)
iii = solve (x^3*(97/10)*x+1 == 6*x-5)
iiia =double(iii)
Plot(ia,iia,iiia)
%this is the code im using but it keeps coming back with the error Incorrect number or types of inputs or outputs for function solve.
%I'm trying to get it to plot i ii and iii into the same graph.
  1 件のコメント
Torsten
Torsten 2024 年 12 月 12 日
I don't understand what you try to do with the x-values defined as x= linspace(-5,5, 11).

サインインしてコメントする。

回答 (1 件)

John D'Errico
John D'Errico 2024 年 12 月 12 日
編集済み: John D'Errico 2024 年 12 月 12 日
Solve does not directly allow you to define a variable as being in a range, but assume will help you out, as I show below. Anyway, this does not do what you think:
syms x i ii iii
x= linspace(-5,5, 11);
whos x
Name Size Bytes Class Attributes x 1x11 88 double
And even though you initially defined x as a sym, x is now a vector of doubles. x is no longer at all symbolic.
As well, you do not need to PREDEFINE i, ii, and iii as symbolic. MATLAB is not c, or a similar language. you do not need to predefine those variables. they are created on the fly when you defined them.
Finally, calling a variable i will introduce problems later on, when you need to use i as it is already predefined, as sqrt(-1).
Solve will try to generate all solutions. You can later on choose only those in the interval you wish to see. Of course, in some cases, there will be infinitely many solutions.
You can use assume of course. (That may not have been obvious. But it does allow you to tell solve "where" to look.)
clear
syms x
x1 = solve(6*x-5 == 97)
x1 =
17
So trying to find a solution to that problem in the interval [-5,5] will be fruitless anyway. There is one unique solution, at x==17.
assume(x >= -5)
assume(x <= 5)
x1 = solve(6*x-5 == 97)
x1 = Empty sym: 0-by-1
But after I told solve about the limits on x, it now sees there are no valid solutions.
Next, consider the quadratic.
x2 = solve(x^2-7*x+3 == 0)
x2 =
7/2 - 37^(1/2)/2
As you can see, while there are normally two solutions to a quadratic, solve now is able to discard one of them, since it falls outside of the interval of interest.
double(x2)
ans = 0.4586

カテゴリ

Help Center および File ExchangeNumeric Solvers についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by