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

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

댓글을 달려면 로그인하십시오.

 채택된 답변

Walter Roberson
Walter Roberson 2012년 11월 18일

0 개 추천

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

Jason
Jason 2012년 11월 18일
Hi Walter - many many thanks. Apologies for the plank-like follow up but (i) i dont generate a plot after running this code and cannot see why (ii) what is "element-by-element division"?
Element-by-element division is
C(J) = A(J) divided by B(J) for each J
Whereas matrix division is the inverse operation to algebra's matrix multiplication C(I,J) = sum(A(I,:) .* B(:,J))
The reason you do not get any plot is that you are misusing linspace(). The third argument to linspace() needs to be the number of points to generate in the range, whereas you have tried to give the interval between points.
Question: you define E0 as the scalar 0, and E0 gets added to things, which obviously leaves them unchanged. But you initialize E to a vector and then overwrite E without having used it. Were you perhaps wanting to add E instead of E0 ?
Jason
Jason 2012년 11월 18일
편집: Walter Roberson 2012년 11월 18일
Hi Walter. Thanks again. I spotted this error and redrafted the code to:
clc
clear all
c=0.056;
e0=0.2;
e=-1:0.1:1;% defines the range of x axis energies over which to run the plot
co=0:0.01:1; %dependent variable spanning concentrations
cr=0:0.01:1; %second dependent variable
co=(cr).*(exp(e-e0)/c);
plot(e,co,'b*-')
xlabel('E potential (V)')
ylabel('x')
The line beginning with "co" still has a syntax error in it - no doubt a matrix thing again but i have tried several iterations to no joy
Walter Roberson
Walter Roberson 2012년 11월 18일
-1:0.1:1 has a different number of values than 0:0.01:1. When you are doing a vector calculation, the vectors must have matched shapes and sizes.
Also note that you define co but overwrite it before using it; that is not a syntax error but indicates a probable error in thinking about the program.
Jason
Jason 2012년 11월 18일
편집: Walter Roberson 2012년 11월 18일
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 2012년 11월 18일
Use .* for element-by-element multiplication.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

질문:

2012년 11월 18일

댓글:

2018년 5월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by