I'm trying to generate a plot of the following equation:
Adon=4.36e7;
Bdon=12.8;
pdon=(0.5:5);
ddon=1e-3;
Vdon=(Adon*pdon*ddon)/(log(pdon*ddon)+Bdon);
plot(pdon,Vdon);
But when I run that, I get a blank figure. No data in the function at all.
In the same .m file, I am able to successfully plot the following:
p=(760:1000);
d=0.1;
T1=273;
B=365;
A=15;
C=1.18;
conch1=293*p*d/(760*T1);
Vb1 = 24.22*conch1+6.08*sqrt(conch1);
plot(p,Vb1);
The only difference seems to be the Log function in the denominator of the former code, but it strikes me as unlikely that Matlab would be unable to plot a log function. It's able to calculate results for discrete values of pdon, but it is not plotting anything.
I have tried focing opengl to use software acceleration, and forcing it to use hardware acceleration. Neither worked.
I have tried commenting out everything but the first bit of code that shouild plot Vdon, and it still returns a blank figure.
I'm new to Matlab, so if there's something that I'm missing I'd be pleased to learn it. Why is the first equation returning a blank figure, but the second equation works as expected. How can I fix it?

 採用された回答

John D'Errico
John D'Errico 2020 年 5 月 1 日
編集済み: John D'Errico 2020 年 5 月 1 日

1 投票

LoL. You are trying to do all sorts of things that are of no value, when your problem was in not appreciating how to divide the elements of two vectors element-wise.
pdon is a vector. This is wrong;
Vdon=(Adon*pdon*ddon)/(log(pdon*ddon)+Bdon);
Instead, do this:
Vdon=(Adon*pdon*ddon)./(log(pdon*ddon)+Bdon);
what a difference a dot makes.
Much of MATLAB is oriented to array operations, which allow you to perform linear algebra efficiently. But if you only want to multiply or divide a list of numbers, you need to use the .* and ./ and .^ operators. As I said, you were looking for something complicated, in the form of opengl, hardware acceleration, etc. This was just basic arithmetic that was the problem. :)

3 件のコメント

Gregory Decker
Gregory Decker 2020 年 5 月 1 日
Not sure laughing at me was necessary, but thanks. That appears to have solved my issue.
Why did pdon being a vector matter in the second equation when p, which is presumably also a vector, did not?
Gregory Decker
Gregory Decker 2020 年 5 月 1 日
I see. Thanks.
John D'Errico
John D'Errico 2020 年 5 月 1 日
編集済み: John D'Errico 2020 年 5 月 1 日
It was just that rather than spend the initial time to learn how to use the basics of MATLAB, you were looking in the wrong place to solve your problem. No matter though.
Your question here is completely valid. But let me start in the beginning. When you add a constant to a vector, MATLAB has no problem:
(1:3) + 5
ans =
6 7 8
MATLAB figures you want to apply that scalar addition to each element of the vector. This is called a scalar expansion.
MATLAB is able to do the same for multiplication by a scalar, in fact, it can even divide a vector or an array by a scalar. Still no problems.
The second case you mention was exactly that. You had a vector that was multiplied and divided by scalars.
However, when you want to perform vector*vector operations or vector/vector or vector^vector ops, you need to use the .* and ./ operators. If you fail to do that, then you will either get something unexpectedly strange, or an error.
(1:3)*(2:4)
Error using *
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the number of
rows in the second matrix. To perform elementwise multiplication, use '.*'.
Whereas:
(1:3).*(2:4)
ans =
2 6 12
As well, a scalar divided by a vector is a problem. You need there to use the ./ operator.
In fact, the vector/vector op "works" sort of.
(1:3)/(2:4)
ans =
0.68966
But it does not give you what you expected. (What it did compute was a least squares thing: what constant can you best multiply the second vector by, to get the best approximation to the first.) This was your first problem. So when you plotted that result, there was only a scalar to plot.
In general, unless you actively want to do array and linear algebraic computations, it is safest to stick with the dotted operators. (There are no .+ and .- operators. No need for them.)

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File Exchange で Matrix Indexing についてさらに検索

製品

リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by