How to set an array in matlab?

조회 수: 3 (최근 30일)
Alex
Alex 2014년 2월 17일
편집: A Jenkins 2014년 2월 17일
Hi, I ma trying to plot a graph Re as a function of k. But before that, I need to input A,B and v to calculate a,b,c,d,alpha and beta.
Below is my code:
clear all
A1=input('What is the value of A ','s');
B1=input('What is the value of B ','s');
V1=input('What is the value of V ','s');
K1=input('What is the value of K ','s');
A=str2num(A1);
B=str2num(B1);
K=str2num(K1);
V=str2num(V1);
a=B; %%%formula for a
b=(A+sqrt(A.^2 - 4*(B.^2)))/(A-sqrt(A.^2 - 4*(B.^2))); %%%formula for b
c= -2*B; %%%formula for c
d=-2*A/(A-sqrt(A.^2 - 4*(B.^2))); %%%formula for d
array(K);
alpha=(a+d-K.^2).^2 - (K.^2)*(V.^2)-4*(a*d - b*c - d*(K.^2))
beta=2*K*V*(K.^2 - a + d)
re=1/2 *(a+d-K.^2+ sqrt((1/2 *((sqrt((alpha).^2 + (beta).^2))+ alpha))))
%A plot Re as a function of k
K=(0:0.01:1);
y=re;
figure;
plot(K,y,'r');
title('Re as a function of k', 'fontsize', 10);
xlabel('K');
ylabel('Re');

답변 (1개)

A Jenkins
A Jenkins 2014년 2월 17일
I'm not really sure what your question is. Are you trying to do this?
clear all
A1=input('What is the value of A ','s');
B1=input('What is the value of B ','s');
V1=input('What is the value of V ','s');
K1=input('What is the value of K ','s');
A=str2num(A1);
B=str2num(B1);
K=str2num(K1);
V=str2num(V1);
a=B; %%%formula for a
b=(A+sqrt(A.^2 - 4*(B.^2)))/(A-sqrt(A.^2 - 4*(B.^2))); %%%formula for b
c= -2*B; %%%formula for c
d=-2*A/(A-sqrt(A.^2 - 4*(B.^2))); %%%formula for d
%array(K);
K=(0:0.01:1);
alpha=(a+d-K.^2).^2 - (K.^2)*(V.^2)-4*(a*d - b*c - d*(K.^2))
beta=2.*K.*V.*(K.^2 - a + d)
re=1/2 *(a+d-K.^2+ sqrt((1/2 *((sqrt((alpha).^2 + (beta).^2))+ alpha))))
%A plot Re as a function of k
y=re;
figure;
plot(K,y,'r');
title('Re as a function of k', 'fontsize', 10);
xlabel('K');
ylabel('Re');
  댓글 수: 2
Alex
Alex 2014년 2월 17일
편집: Alex 2014년 2월 17일
Sorry of poor explaining. All I want to do is plot the graph a graph of "Re" as a function of "K". First, I need to input the values of A1,B1 and V1 to calculate a,b,c,d, alpha and beta.
But perhaps the problem is that I have already defined K as a scalar, and then Re becomes a scalar. Then I redefine K as a vector, but I do not think that matlab will recompute Re so y just becomes the scalar.
The codes above is my present works, it works fine until the code attempt to plot the graph.
A Jenkins
A Jenkins 2014년 2월 17일
편집: A Jenkins 2014년 2월 17일
My version of the code (pasted above) does this:
Is this what you want?
(I'm not sure why you are accepting K as an input, as far as I can tell, you could just remove that code.)

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

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by