필터 지우기
필터 지우기

How can I create expression or vector function handle, then plot it ?

조회 수: 1 (최근 30일)
work wolf
work wolf 2017년 12월 8일
댓글: work wolf 2017년 12월 12일
How can I create vector function handle (anonymous function) with product a scalar value, then plot result. Clearly, I want to plot this formula
Let f(x,ym)= (1-exp(x-ym)/h); and
iX(x)=1/6.f(x,ym(1))+
7/6.f(x,ym(2))+
25/6.f(x,ym(3))+
43/6.f(x,ym(4));
iY(x)=1/5.f(x,ym(1))+
2/5.f(x,ym(2))+
14/5.f(x,ym(3))+
4/5.f(x,ym(4));
This code without loop but give me: ''Error using plot Conversion to double from sym is not possible''.
clear all;clc;a=0;b=2;n=3;h=(b-a)/n;ym=a:h:b;
X=[ 1/6, 7/6, 25/6,43/6]; % Data
Y=[ 1/5, 2/5, 14/5,14/5];
f=@(x,ym) (1-exp(x-ym)/h);
% with syms
syms x;
iX=f(x,ym(1:length(ym))).*X; % result iX= [f.1/6 , f.7/6 , f.25/6 , f.43/6]
iY=f(x,ym(1:length(ym))).*Y;
iFx= sum(iX);iFy=sum(iY); % result iFx= f.1/6 + f.7/6 + f.25/6 + f.43/6
x=ym;
plot(x,iFx,'r+');hold on;plot(x,iFy,'og');
Moreover, I tired to use a alternate codes with loop condition but the error persists as conversion to logical from sym is not possible.
% clear all;clc
% a=0;b=2;n=3;h=(b-a)/n;ym=a:h:b;
% %
% X=[ 1/6, 7/6, 25/6,43/6]; % Data
% Y=[ 1/5, 2/5, 14/5,4/5];
% %
% f=@(x,ym) (1-exp(x-ym)/h);
% %
% syms x %double(subs(x))
% iX=cell(1,length(ym));iY=cell(1,length(ym)); % cell, iX{k} and iY{k}
% % iX=zeros(1,length(ym));iY=zeros(1,length(ym));
% for k=1:length(ym)
% % iX(k) = f(x,ym(k)).*X(k);
% % iY(k) = f(x,ym(k)).*Y(k);
% iX{k} = @(x,k) f(x,ym(k)).*X(k);
% iY{k} = @(x,k) f(x,ym(k)).*Y(k);
% iX(x,k);
% end
% whos
% x=ym;
% plot(x,iX{:},'r:');hold on;plot(x,iY{:},'b--');
please, how can i fix it?
  댓글 수: 2
Stephen23
Stephen23 2017년 12월 11일
@work wolf: why do you need to use a symbolic variable?
work wolf
work wolf 2017년 12월 11일
편집: work wolf 2017년 12월 11일
Hello, Stephen Cobeldick :). I don't need to use a symbolic variable! I want to create a function with product data as scalar elements (constant), then plot it as I mentioned above, either codes are some attempts. I hope it's more clear now. If you have any idea. please, do not hesitate to present it.

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

답변 (1개)

Walter Roberson
Walter Roberson 2017년 12월 11일
plot(x,subs(iFx))
  댓글 수: 1
work wolf
work wolf 2017년 12월 12일
@ Walter , Thanks .
subs() or matlabFunction(),
take too much time. In particular, at a long interval as ym=linspace(a,b,50000) or n large.

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

카테고리

Help CenterFile Exchange에서 Conversion Between Symbolic and Numeric에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by