Simulating a simple equation

2 Ansichten (letzte 30 Tage)
Jason
Jason am 18 Nov. 2012
Kommentiert: John BG am 6 Mai 2018
I am new fairly new to Matlab - a light user for a few years and know only the basics.
I am trying to use it to simulate a trend in two parameters with a third and to then plot the result.
All i want to do, at least to start with, is to simulate the trend in Co and Cr with E according to:
c=0.057;% a constant
E0=0;
E=-0.5:0.05:0.5;
Co=linspace(0,1,0.01); %dependent variable
Cr=linspace (0,1,0.01); %second dependent variable
E=E0+(c*log(Co/Cr));
plot(E,Co,'b*-')
i think lines 3&4 are equivalent ways of specifying a range of numbers.
This fails with an error related to the vectors being of unequal length. f, by "plotting vectors" the software means E and Co then i don't understand why these would need to be "the same length" (indeed this error remains if i confine them to the same numerical range anyway).
My questions are:
(i) am i going about this supposedly simple task in the right way and (ii) what specifically am i doing wrong
any help hugely appreciated
  1 Kommentar
John BG
John BG am 6 Mai 2018
Hi Jason
1.
you are plugging the function output E into what plot expects to be the reference vector of the plotting, therefore, the plot shows up a vertical line:
c=0.057; % obvious constant
E0=0;
E=-0.5:0.05:0.5;
Co=[.01:.01:1]
Cr=[.01:.01:1]
E=E0+(c*log(Co./Cr));
plot(E,Co,'b*-')
so instead of
plot(E,Co,'b*-')
E, the function, has to be the 2nd input field of plot, or the only input field of plot, setting parameters aside.
plot(Co,E,'b*-')
2.
This is still pretty flat, because Co==Cr
isequal(Co,Cr)
ans =
logical
1
don't you really mean E to be something like this?
c=0.057;% a constant
E0=0;
E=-0.5:0.05:0.5;
Co=[.01:.01:1]
Cr=[.01:.01:1]
E=E0+(Co.*log(c./Cr));
plot(Co,E,'b*-');grid on

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 18 Nov. 2012
In your line
E=E0+(c*log(Co/Cr));
the / operation is matrix division, not element-by-element division. Change the line to
E = E0 + (c * log(Co ./ Cr));
  6 Kommentare
Jason
Jason am 18 Nov. 2012
Bearbeitet: Walter Roberson am 18 Nov. 2012
Hi Walter. You are patient, thanks.
i have sorted the above to:
c=0.056;
e0=0.2;
e=-1:0.1:1;
cr=0:0.05:1;
co=cr*exp((e-e0)/c);
this still fails specifically because of the cr element but i dont know why if the exponential term also comprises 20 values
Walter Roberson
Walter Roberson am 18 Nov. 2012
Use .* for element-by-element multiplication.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by