I have a function and two values of x and I would like to find the values corresponding to the x-values, how should I start ? I was thinking to do for loop but it does not work it only gave me one of the answers (as you can see below)!
for x = 0.36 : 0.40
y = x^4 + 5*x^2 + sqrt(x-1)
end
y =
0.6648 + 0.8000i

1 件のコメント

John D'Errico
John D'Errico 2017 年 1 月 9 日
So your code was wrong. Show your code, and someone will show you how to fix it.

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

 採用された回答

John D'Errico
John D'Errico 2017 年 1 月 9 日

1 投票

Ok, you posted your code.
As I expected, every time through that loop, you overwrote the previous values.
Worse, what do you think 0.36:0.40 does? It generates a set of values from 0.36 to 0.40 in increments of 1 (ONE)! How many values do you think that creates? ONE: 0.36.
Simplest is to not use a loop.
x = [0.36, 0.40];
y = x.^4 + 5*x.^2 + sqrt(x-1);
Note the use of .^ there. It is there for a reason. Learn about element-wise operations on a vector or array.
help power

4 件のコメント

Yumi Lee
Yumi Lee 2017 年 1 月 9 日
編集済み: Yumi Lee 2017 年 1 月 9 日
OMG ! so using loop was useless lol Thank you so much that helps a lot :)
Yumi Lee
Yumi Lee 2017 年 1 月 9 日
But why this does not work if I have something such as (x-1) multiply by the square root ? It says '' Error using * Inner matrix dimensions must agree.'' Any idea ?
Stephen23
Stephen23 2017 年 1 月 9 日
編集済み: Stephen23 2017 年 1 月 9 日
Yumi Lee
Yumi Lee 2017 年 1 月 11 日
ok I will try it. Thank you

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

2017 年 1 月 9 日

コメント済み:

2017 年 1 月 11 日

Community Treasure Hunt

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

Start Hunting!

Translated by