How to plot a function for different values of input parameter in a function

Hi,
Please, how do I create a procedure/function such that it carries \xi as an input and use it to plot r for different values of \xi. I have attached my code.

回答(1 个)

Just define xi as a column vector of values, e.g.
xi = [0.03 0.06].'; % activity parameter

11 个评论

Thank you. But how do I call it in a function r then? For instance, if I want to plot for both 0.03 and 0.06 on the same figure?
I don't understand.
In your code, both graphics (for xi=0.03 and xi =0.06) appear in the same figure if you leave the rest of your code unchanged.
I received this erro: Arrays have incompatible sizes for this operation.
Error in qfordifferentxi (line 21)
r=q-(1-alpha)*tan(q)+(alpha3*xi*alpha/eta1)./(4*k1*q.^2/d^2-alpha3*xi/eta1).*tan(q);
Did you use xi as column vector ?
Try what you get with elementwise multiplication of a column vector with a row vector.
r = rand(1,3);
xi = rand(2,1);
r.*xi
ans = 2×3
0.3904 0.0027 0.3068 0.2633 0.0018 0.2069
%% initialization
clear
clc
close all
%% Model parameters
global k1 eta1 alpha3 gamma1 d xi
k1 = 6*10^(-12); % elastic constant
eta1 = 0.0240; % viscosity
xi = [0.03 0.06 0.09].'; % activity parameter
alpha3 = -0.001104; % viscosity
gamma1 = 0.1093; % viscosity
Theta = 0.0001;
d = 0.0002;
%% r(q) from matlab
alpha=1-alpha3^2/gamma1/eta1;
q=0:0.01:(5*pi);
r=q-(1-alpha)*tan(q)+(alpha3*xi*alpha/eta1)./(4*k1*q.^2/d^2-alpha3*xi/eta1).*tan(q);
% Plotting r(q)
figure
plot(q,r)
xlabel('q')
ylabel('r(q)')
axis([0 5*pi -20 20])
I don't understand. My answer was
Just define xi as a column vector of values, e.g.
xi = [0.03 0.06].'; % activity parameter
Is working now. Thank very much. I didn't define it as a row vector.
xi = [0.03, 0.06] is a vector, but a row vector.
I mean I didn't do that before. Is actually working now.
But I tried to apply the same to my general code, it failed.
%% initialization
clear
clc
close all
%% Model parameters
global k1 eta1 alpha3 gamma1 d N Phi xi h A B C G
k1 = 6*10^(-12); % elastic constant
eta1 = 0.0240; % viscosity
xi = [-0.02 0.01 0.0].'; % activity parameter
alpha3 = -0.001104; % viscosity
gamma1 = 0.1093; % viscosity
Theta = 0.0001;
d = 0.0002;
%% Extract r(q) data from maple
%rqdata= xlsread('rqdata.xlsx');
%rq= rqdata(:,2);
%qsize = rqdata(:,1);
%% r(q) from matlab
alpha=1-alpha3^2/gamma1/eta1;
q=0:0.001:(5*pi);
r=q-(1-alpha)*tan(q)+(alpha3*xi*alpha/eta1)./(4*k1*q.^2/d^2-alpha3*xi/eta1).*tan(q);
% Plotting r(q)
%figure
%hold on
%plot(qsize,rq, 'r', MarkerSize=2)
plot(q,r, 'b--', MarkerSize=2)
xlabel('t (seconds)')
ylabel('\theta(d/2,t)(rad)')
legend('maple', 'matlab')
axis([0 5*pi -20 20])
Thank you Torsten. This is actually working fine. But I have a different problem now. This is just part of my codes. I didn't wanna post all code. I thought using this as an instance could solve my problem.
In the attached file,I also wanna plot theta_sol and v_sol for xi = [0.03 0.06].'; but I received an error: Error in Case1_Ijuptilk_130822>lcode1 (line 147): rhsode(N,1) = (G/(h^3))*(-Phi + 3*theta(1) - 3*theta(2) + theta(3)) + (C/(h^2))*(v(2) -2*v(1)+ 0)
Please could you me check it out.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Axis Labels 的更多信息

产品

版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by